bluegray Posted June 2, 2010 Share Posted June 2, 2010 Hi Php freaks =). $testbox = isset($_POST['testbox'])?$_POST['testbox']:null; I need to know what the "?" and the ":" are and how they work. Greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/ Share on other sites More sharing options...
ram4nd Posted June 2, 2010 Share Posted June 2, 2010 if(isset($_POST['testbox'])) $testbox = $_POST['testbox']; else $testbox = null; Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1066735 Share on other sites More sharing options...
lostnucleus Posted June 2, 2010 Share Posted June 2, 2010 the above code is same as if(isset($_POST['testbox'])) { return $_POST['testbox']; }else { return null; } if the code behind ? , returns true then $textbox is set to wat comes just after ? , if it return false then $textbox is set to wat comes after : . Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1066738 Share on other sites More sharing options...
ram4nd Posted June 2, 2010 Share Posted June 2, 2010 Yes description is nice, but you wrote wrong code, mine is right. instead of your returns there should be $testbox = Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1066760 Share on other sites More sharing options...
bluegray Posted June 2, 2010 Author Share Posted June 2, 2010 if(isset($_POST['testbox'])) $testbox = $_POST['testbox']; else $testbox = null; Perhaps I asked my question incorrectly. I understand how the code functions, but I've never seen the "?" or ":" defined or even used before. What's the purpose of the if, else statement if we have "?" and ":" to use. Additionally, I've learned whatever I know from Php books, yet have never seen these two operators offered as an alternative to the "if else" statement. Is there a reason for this? Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1066767 Share on other sites More sharing options...
ram4nd Posted June 2, 2010 Share Posted June 2, 2010 it makes the code shorter, but harder to read. Depends on taste. Why it exists, the question is rather why not. Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1066773 Share on other sites More sharing options...
premiso Posted June 2, 2010 Share Posted June 2, 2010 It is called the Ternary Operator. Which as stated, it acts as a shortened if / else. As for why it exists, it provides it uses. Namely, for the code you shown, which coincidently I wrote, allows for you to handle invalid / not passed in variables a bit easier without having to do a full on if statement. Some people prefer to use the full on if statement, but for simple assignments, why not use ternary? It does not detract from the code and a full on if statement is not usually needed as all you want to do is make sure that variable has a value assigned, namely for error testing purposes. $name = isset($_POST['name'])?$_POST['name']:null; $address = isset($_POST['address'])?$_POST['address']:null; // VS: if (isset($_POST['name'])) $name = $_POST['name']; else $name = null; if (isset($_POST['address'])) $address = $_POST['address']; else $address = null; As stated, some people do not like it. I like it for assignments like the above, as it just makes sense to me to do it as such. Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1066786 Share on other sites More sharing options...
lostnucleus Posted June 2, 2010 Share Posted June 2, 2010 @r4nd yup return shd be replaced with the var name One particular use of this snippet comes handy with phtmls , where you are coding view part of MVC , <?= isset($this->memberName) ? $this->memberName : 'Welcome Guest'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1066809 Share on other sites More sharing options...
sspoke Posted June 2, 2010 Share Posted June 2, 2010 it should be used when it has to be used thats all. It's not a oh I'll use this instread of if statements.. this is for shorting code that usually ends with a return call like fixing little things like money tax $moneytax = ($money > 400) ? $money * 10 : 0; shit like that but you see it always returns data.. thats why there is always a equals sign before it starts.. if statements don't return anything and in a if statment you'll have to declare $moneytax 2 times! blah yah it doesnt make anything faster it makes code easier to read imo. Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1066837 Share on other sites More sharing options...
ram4nd Posted June 3, 2010 Share Posted June 3, 2010 @r4nd yup return shd be replaced with the var name One particular use of this snippet comes handy with phtmls , where you are coding view part of MVC , <?= isset($this->memberName) ? $this->memberName : 'Welcome Guest'; ?> Word is that its not good to use <?= and <?, you should always use <?php. I use template parser if I want to take advantage of View part in MVC. Because only then HTML and PHP are truely separated. Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1067011 Share on other sites More sharing options...
ignace Posted June 3, 2010 Share Posted June 3, 2010 @r4nd yup return shd be replaced with the var name One particular use of this snippet comes handy with phtmls , where you are coding view part of MVC , <?= isset($this->memberName) ? $this->memberName : 'Welcome Guest'; ?> That depends on how you model your application. I, for one always have a User object with the default name being Guest and rights set accordingly. So I would just write: <?php echo 'Welcome, ', $this->memberName; ?> Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1067114 Share on other sites More sharing options...
Daniel0 Posted June 3, 2010 Share Posted June 3, 2010 @r4nd yup return shd be replaced with the var name One particular use of this snippet comes handy with phtmls , where you are coding view part of MVC , <?= isset($this->memberName) ? $this->memberName : 'Welcome Guest'; ?> Word is that its not good to use <?= and <?, you should always use <?php. It's perfectly fine using those if you have control over the environment(s) that your PHP script will run it. Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1067124 Share on other sites More sharing options...
ignace Posted June 3, 2010 Share Posted June 3, 2010 It's perfectly fine using those if you have control over the environment(s) that your PHP script will run it. In case you don't understand by what he means with environment(s): you have access to the php.ini to configure allow_short_tags. However for portability and ease it's best to use <?php as this is guaranteed to always work. Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1067132 Share on other sites More sharing options...
ram4nd Posted June 3, 2010 Share Posted June 3, 2010 I think he understands. And I get it if you change server you can simply run find and replace, but I guess it's my own weirdness. My point is that standards are good, thats just what i mean by that. Quote Link to comment https://forums.phpfreaks.com/topic/203656-can-someone-explain-two-snippets-of-code/#findComment-1067148 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.