Jump to content

Can you help me explain this....


Rollins

Recommended Posts

i was trying to create a function....first time so i thought i do a simple " if isset echo" thing.

<?ini_set('display_errors', 1);error_reporting(E_ALL); $var_jimmyjoy = 'jimmyjoy'; //functionfunction ifisset($message){if(isset($message)){	echo $message;   }} echo "line 1 is echo : ".$var_jimmyjoy."\n"; // echo var (control)echo "<br>\n";echo "line 2 is echo function : ";ifisset($var_jimmyjoy);"\n"; //echo var through function?>

that actually worked....but on the first attempt i wrote something that was rather lazy coding i think....

 $var_jimmyjoy = 'explain this....jimmyjoy'; echo "line 1 is echo : ".$var_jimmyjoy."\n"; // echo var (control)echo "<br>\n";echo "line 2 is echo function : ".ifisset($var_jimmyjoy)."\n"; //echo var through function

the output confused me...and still does as the order of the text gets shuffled....the variable gets put before the regular text.

line 1 is echo : explain this....jimmyjoy<br>explain this....jimmyjoyline 2 is echo function :

i could understand it would give a syntax error because of bad coding. the way i look at the order of how things should get processed and displayed it should not do that. been keeping be busy for a day now and couldn't find anything on this on searches...Can you help me explain this.... :)

Edited by Rollins
Link to comment
Share on other sites

The echo have to display the concatenation of the string "line 2 is echo function :" with the return value of the ifisset() function and the string "\n".For knowing what is the return value of the ifisset() function, the ifisset() function is launch.The ifisset display the content of its parameter and return a null value. (first output)The strings are concatenate with the null value (null value is evaluated as a void string);The echo display the contatenated string. (second output).

echo "line 2 is echo function :";ifisset($var_jimmyjoy);echo "\n";//will output the text in the desired order.

Edited by Raspberry
Link to comment
Share on other sites

so the server reads the "whole line" and the function within that line. due to incorrect use of quotation marks the function gets put out first...because of the returned NULL? my next question is why does the/a/this function put out a NULL value? default behaviour? thanks for the example code! i got to that to...easier to read. Thanks for replying!

Link to comment
Share on other sites

While all the above is correct, you can also use commas to fix this.

echo "line 2 is echo function : ", ifisset($var_jimmyjoy), "\n";

Although, if your function is echoing data, it's very likely you're doing something wrong. Edit: To answer your last question, yes, all functions return void (null) if you're not returning anything yourself.

 function ifisset($message){    return $message ?: '';}

Edited by Nico
Link to comment
Share on other sites

Note that in your original function, $message is always going to be set. It is defined as a function parameter, so that variable will always be set. The variable that you pass to the function might not be set, but inside the function $message will always be set.

Link to comment
Share on other sites

So it can be replaced by a call to is_null if he really wants to check if the variable was set to the NULL constant. But like I said, the variable will always be set. It's even required, so you can't call that function without passing something to it.

Link to comment
Share on other sites

I'm not sure why you're making such a big deal out of this. There's absolutely nothing wrong with using isset() like that. And isset() is about 5 times faster than is_null() since it's a language construct and not a function.

Edited by Nico
Link to comment
Share on other sites

This is not making a big deal. I'm trying to point out that if he is intending to make a function to check if a variable is set, that's not the way to do it. We're here to help people learn. If you want to check if a variable from another scope is set inside a function that it gets passed to, then the variable needs to be passed by reference. Passing it by value will result in an undefined variable notice, even though isset will return false. This code does not produce errors, because the variable is passed by reference:

<?php ini_set('display_errors', 1);error_reporting(E_ALL); function echo_if_set(&$var){  if (isset($var))  {    echo 'variable is set';  }}  echo_if_set($undefined); $foo = 'bar';echo_if_set($foo);

Like I said, the point is to learn. There's no reason to teach people practices that are going to lead to error messages, even if they are just notices. The goal is error-free code. Remove that ampersand above to have the variable passed by value, and you get an undefined variable notice.

Link to comment
Share on other sites

dunno if it is proper testing but....

function ifisset($message){if(isset($message)){    echo $message;   }   else   {   $message = 'no data received';   echo $message;   }   }

and

function ifisset($message){if(IS_NULL($message)){    $message = 'no data received';   echo $message;   }   else   {   echo $message;   }   }

both put out 'no message received' when i remove the original variable. they also put out a notice of a undefined variable....when i set the variable to $var ='NULL' they also echo 'no message received' both without the notice...the way i look at it both comparisons function the same or i must have missed something in my noobishness.... not sure what to conclude from the notices though....

Link to comment
Share on other sites

the question got answered before it was asked....nice. :) and the original question was about the behaviour of the function. not specificly about the isset() or is_null(). although it is good to know.... thanks for the replies! all of you...

Link to comment
Share on other sites

Try this, when you pass the variable by reference instead of value:

function ifisset(&$message){  if(isset($message)){	echo $message;  }  else  {	echo 'no data received';  }}

Notice the ampersand before the parameter name, that tells it that the parameter is passed by reference. When you pass by value (without the ampersand), when you execute the function then PHP will send only the value stored in the variable to the function. When you pass by reference then you pass the actual variable itself, meaning that any changes you make to the variable inside the function will also be in effect outside the function. Consider the following code, the inc_var2 function is pass by reference, which causes the variable $foo to get incremented so that after the function ends its value has changed:

function inc_var($var){  $var = $var + 1;  echo $var;} function inc_var2(&$var){  $var = $var + 1;  echo $var;} $foo = 1; inc_var($foo); // pass $foo by valueecho $foo; // echos 1, $foo is unchanged inc_var2($foo); // pass $foo by referenceecho $foo; // $foo is now 2

Note that you only define passing by reference when you define the function, not when you pass a variable to it. Calling the 2 functions looks the same, but one of them was defined to have the variable passed by reference. PHP uses that for several built-in functions. The sort function, for example, which sorts an array, only returns a true/false value. The array was passed by reference and sorted in-place, and the function returns a value to say whether the sort was successful. http://www.php.net/m...nction.sort.php You can see in the description of the function that the variable gets passed by reference: bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

Link to comment
Share on other sites

thanks for that example! that also means there is a distinct difference between a variable and the content of a variable....i always looked at them as just one thing. i think i will look at the sort functions at a later point....

Link to comment
Share on other sites

Sort was just an example of a built-in function that uses pass-by-reference, there are several others though. You can find them in the manual because they show an ampersand character before the parameter name, meaning that parameter is being passed by reference. You can think of a variable like a box or container. What is inside the box is the variable's value. When you're passing by value you copy the contents and use that (leaving the original variable unchanged), when you pass by reference you pass the whole box, so that any changes made happen to the actual variable. In college they used a mailbox analogy, where you can think of a variable as a mailbox with an address (that analogy works better for languages like C which actually reserve space in memory for individual variables, PHP does not handle variables like that). In C, passing by reference means that you're sending the actual memory address of the variable, rather than the value in memory. C contains variable types called pointers, which just hold addresses. You can do things like binary math on the pointer to move around in memory, or you can "derefence" the pointer to get at the value at that address in memory. PHP doesn't have pointers because PHP abstracts the memory system away from the programmer, but passing variables by reference typically accomplishes the same types of things.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...