top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can I get a variable into my function to retain its value during calls?

+1 vote
385 views
How can I get a variable into my function to retain its value during calls?
posted Jun 18, 2014 by Amritpal Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You need to use the static keyword in your function declaration, so for instance:
<?php function myfunc() { static $myfunctionvariable = 1; echo "the number $myfunctionvariable\n"; $myfunctionvariable++; } myfunc(); //1 myfunc();//2 myfunc();//3 ?>

The use of the static keyword is helping to assign the value 1 once into the variable, and each time function is called,the number is incrementing its value by 1.

answer Jun 19, 2014 by Karamjeet Singh
Similar Questions
0 votes
for($i=0;$i<=feof($getdata);$i++)
{
if (filter_var($data[$i][1], FILTER_VALIDATE_EMAIL)){
echo $data[$i][1];
$email=$data[$i][1];
$conn = mysqli_connect($dbhost,$dbuser,$dbpass, $dbname);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$sql ="INSERT INTO promo_user (uid,name,email) VALUES (,'', '$email')";
mysqli_query($sql,$conn);
mysqli_close($conn);

I am using the above code but there is something wrong with it,whenever i run the code the echo is working fine but the content does go into sql table

Please help

0 votes

PHP:: Suppose I am trying to assign a variable the value of 0123, but it keeps coming up with a different number, whats the problem?

...