Jump to content

what is this code doing?


dotolee

Recommended Posts

Hi there.  I'm a newbie to PHP.  I've run across a line of code that I don't quite understand.  Hopefully this is a remedial question.

 

Here's the line of code:

$mymess = isset($_GET['ms'])?$_GET['ms']:false;

 

it looks it's trying to get a string from the querystring / URL called "ms" and save it to a new variable called mymess IF it's not null.  But i don't quite get the "second" part of the line from the "?" on.  Does it save a "false" value to the mymess if isset is false?  I'm using the PHP manual i downloaded from the php site but I haven't been able to find the answer ..

thanks.

 

Link to comment
Share on other sites

Hey!  Thank you very much for the quick reply. 

So basically, it sounds like the "second" part of the line of code is just initializing the variable to false to start.

Thanks for the example of the "ms" variable.  That part I do understand - I guess I just didn't use the right lingo.  but i do understand that it's a var saved /passed in the querystring.

 

Thanks again.

Link to comment
Share on other sites

Np,

 

The "?" is part of the Ternary FORMAT, that's the format that must be used in order to do such an operator.

 

Just follow this format. Its saying, if the name == Chris, then make the value of $someVar $name, otherwise set the value Chris.

 

$name = "Chris";
$someVar = ($name == "Chris") ? $name : "Chris";

 

this equals to

 

$name = "Chris";
$someVar = "";
if($name == "Chris"){

$someVar = $name;

}else{

$someVar = "Chris";

}

 

To learn more about the Ternary operator use the link I provided in my post above.

 

Please Mark Topic As Solved. Thank You

Link to comment
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.