dotolee Posted August 5, 2011 Share Posted August 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
dotolee Posted August 5, 2011 Author Share Posted August 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
dotolee Posted August 5, 2011 Author Share Posted August 5, 2011 oh.. one other question... so is it the "?" embedded in the middle that separates these two lines of code? what is this "?" called? i'd like to do some more reading on it if possible. thanks. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
voip03 Posted August 5, 2011 Share Posted August 5, 2011 phpSensei Thank U http://www.addedbytes.com/lab/ternary-conditionals/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.