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
https://forums.phpfreaks.com/topic/243883-what-is-this-code-doing/
Share on other sites

This is no different than saying

 

<?php

$mymess = false;
if(isset($_GET['ms'])){

$mymess = $_GET['ms'];

}
?>

 

Its called the Ternary Operator

 

http://php.net/manual/en/language.operators.comparison.php

 

The URL isn't called MS... MS is a variable in the HTTP URL.

 

for example

 

http://www.website.com/index.php?ms=somevalue

 

 

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.