Showing posts with label value. Show all posts
Showing posts with label value. Show all posts

Saturday, June 18, 2016

Using the static Statement to Remember the Value of a Variable Between Function Calls


If you declare a variable within a function in conjunction with the static statement, the variable remains local to the function, and the function "remembers" the value of the variable from execution to execution





<?php
function numberedHeading($txt) {
static $num_of_calls = 0;
$num_of_calls++;
echo "<h1>".$num_of_calls." ". $txt."</h1>"; }
numberedHeading("Mobile Phones");
echo "<p>We build a fine range of mobile phones.</p>";
numberedHeading("Camera");
echo "<p>Also Digital Cameras..</p>";
?>

The numberedHeading() function has become entirely self-contained. When we declare the $num_of_calls variable on line 3, we assign an initial value to it. This assignment is made when the function is first called on line 7. This initial assignment is ignored when the function is called a second time on line 9. Instead, the code remembers the previous value of $num_of_calls. We can now paste the numberedHeading() function into other scripts without worrying about global variables.

Friday, May 13, 2016

How To Add Radio Button Value In Database In PHP

Today I will share a working script example which will help you to insert radio button value in database using PHP.

<?php
if(isset($_POST[submit]))
{
$number=$_POST[number];
if($number!="")
{
$query=mysql_query("UPDATE users SET number = $number WHERE name = ".$user->name.";");
if($query)
{
header(Location: /me);
}
else
{
$error_message = <font color="red"><strong>Please choose a number.</strong></font><br /><br />;
}
}
}
?>


<form method="post" action="">
<input type="radio" name="number" value="1" />1
<input type="radio" name="number" value="2" />2
<input type="radio" name="number" value="3" />3
<input type="submit" name="submit" />
</form>
?>

This simple script will enable you to add radio button values in database easily.Hope you found this post useful.

Enjoy!