Jump to content

Recommended Posts

Hi,

I'm currently learning about PHP functions. I wrote this function out:

 


<?php


function myfuction(){


static $a = 0;

++$a;

if($a = 1){

echo "this function has been loaded " .$a. " time<br />";

} else {


echo "this function has been loaded " .$a ." time(s)<br />";

}

}

for($i = 0; $i <= 8; ++$i){

myfuction();

}


?>

 

this returns

 

this function has been loaded 1 time
this function has been loaded 1 time
this function has been loaded 1 time
this function has been loaded 1 time
this function has been loaded 1 time
this function has been loaded 1 time
this function has been loaded 1 time
this function has been loaded 1 time
this function has been loaded 1 time

 

My goal was to make it where the function checks if $a = 1 and if it does.. makes the line singular.

Link to comment
https://forums.phpfreaks.com/topic/136373-function-problem/
Share on other sites

you are resetting the number to 0 every time on line one of your function i think though i'm not familiar with "static".


<?php


function myfuction(){


if(!isset($a)){ $a = 0; }

++$a;

if($a == 1){

echo "this function has been loaded " .$a. " time<br />";

} else {


echo "this function has been loaded " .$a ." time(s)<br />";

}

}

for($i = 0; $i <= 8; ++$i){

myfuction();

}


?>

Link to comment
https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711500
Share on other sites

you are resetting the number to 0 every time on line one of your function i think though i'm not familiar with "static".


<?php


function myfuction(){


if(!isset($a)){ $a = 0; }

++$a;

if($a == 1){

echo "this function has been loaded " .$a. " time<br />";

} else {


echo "this function has been loaded " .$a ." time(s)<br />";

}

}

for($i = 0; $i <= 8; ++$i){

myfuction();

}


?>

 

Brian brings up a great point, Function variables are local to that function.

 

After looking up static

 

This function is quite useless since every time it is called it sets $a to 0 and prints "0". The $a++ which increments the variable serves no purpose since as soon as the function exits the $a variable disappears. To make a useful counting function which will not lose track of the current count, the $a variable is declared static:

 

Example #5 Example use of static variables

<?php

function test()

{

    static $a = 0;

    echo $a;

    $a++;

}

?>

 

Now, $a is initialized only in first call of function and every time the test() function is called it will print the value of $a and increment it.

 

Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The following simple function recursively counts to 10, using the static variable $count to know when to stop:

 

Example #6 Static variables with recursive functions

<?php

function test()

{

    static $count = 0;

 

    $count++;

    echo $count;

    if ($count < 10) {

        test();

    }

    $count--;

}

?>

 

You need to make the function recursive for it to work. If you do not want to make it recursive, this should work.

 


<?php


function myfuction(){
global $a;

++$a;

if($a == 1){

echo "this function has been loaded " .$a. " time<br />";

} else {


echo "this function has been loaded " .$a ." time(s)<br />";

}

}

$a=0;
for($i = 0; $i <= 8; ++$i){

myfuction();

}


?>

 

That should get you the desired result.

Link to comment
https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711504
Share on other sites

They are right, you were assigning (=) a to 1 every time you looped through.  You need to compare (==) a to 1.  Why not pass $i to the function in the for loop?

 

function myfuction($a){
$a+=1;
echo ($a==1) ? "this function has been loaded " .$a. "time
" : "this function has been loaded " .$a ." time(s)
"
}

for($i = 0; $i 	myfuction($i);
}
?>

 

FYI:  You spelled function wrong.

Link to comment
https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711509
Share on other sites

premiso:

If you use global inside the function, variable's scope will extend outside the function.  The whole point of static variables is that they are 'remembered' after function is executed, but retain the function scope.  In addition, recursion is when something calls itself, which is not what you're doing in your solution.

 

justinh:

Just thought I'd point out that grammatically, putting (s) at the end of something makes it optional, so technically, you would have time(s) no matter how many times the function is called.  In your if..else condition, you would want to display 'time' or 'times'; no parenthesis. Just a minor bitch from the grammar police.

<?php
function myfuction(){
  static $a = 1;

  if ($a == 1) {
    echo "this function has been loaded " .$a. " time<br />";
  } else {
    echo "this function has been loaded " .$a. " times<br />";
  }
  $a++;
}

for($i = 0; $i <= 8; ++$i){
  myfuction();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711517
Share on other sites

premiso:

If you use global inside the function, variable's scope will extend outside the function.  The whole point of static variables is that they are 'remembered' after function is executed, but retain the function scope.  In addition, recursion is when something calls itself, which is not what you're doing in your solution.

 

Yea, I saw my mistake after the time ran out to edit. But I did not offer up a recursive solution, " If you do not want to make it recursive, this should work."

 

Either way that post is very misleading, as he had the static usage right and I mis-read the php page thinking that it was referring to the lower example as appose to the upper one when it said this would not work.

 

But yea, thanks for pointing it out to him.

Link to comment
https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711521
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.