tag:blogger.com,1999:blog-865923359735383241.post2437458738519358705..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: What is the PHP ? : operator called and what does it do?Unknownnoreply@blogger.comBlogger8125tag:blogger.com,1999:blog-865923359735383241.post-13407489278010663212012-04-12T03:16:00.386-06:002012-04-12T03:16:00.386-06:00That is a one line if statement:
condition ? true...That is a one line if statement:<br /><br />condition ? true : false<br /><br /><br />Translated to an ordinary if statement in your case, that would be:<br /><br />if($request_type == 'SSL') HTTPS_SERVER;<br />else HTTP_SERVER;Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-64847705535526188972012-04-12T03:15:58.150-06:002012-04-12T03:15:58.150-06:00As John T says, it is called a ternary operator an...As John T says, it is called a ternary operator and is essentially a shorthand version of an if /else statement. Your example, as a full if / else statement, would read;<br /><br />if($request_type == 'SSL')<br />{<br /> HTTPS_SERVER;<br />}<br />else<br />{<br /> HTTP_SERVER;<br />}Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-16081995828670878832012-04-12T03:15:57.432-06:002012-04-12T03:15:57.432-06:00That's basically a fancy way if writing an if ...That's basically a fancy way if writing an if else statement.<br /><br />Some say its easier to read, some say not. <br /><br />ternary operator at wikipediaUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-3975948548964658732012-04-12T03:15:56.844-06:002012-04-12T03:15:56.844-06:00Conditional operator ?: is an operator which is us...Conditional operator ?: is an operator which is used to check a condition and select a value depending on the value of the condition. It is expressed in the following form:<br /><br /> variable = condition ? expression1 : expression2;<br /><br /><br />It works as follows...<br /><br /><br />Firstly, condition is evaluated.<br />If the condition is true, then expression1 is evalauated. And the value of expression1 is assigned to the variable.<br />If the condition is false, then expression2 is evaluated. And the value of expression2 is assigned to the variable.<br /><br /><br />For example:<br /><br /> x = (a>b) ? 5 : 9<br /><br /><br />In this, for x, firstly the condition (a>b) is evaluated. If this condition becomes true, then x will become the value 5 (ie, x=5). But if the condition (a>b) becomes false, then x will attain the value 9 (ie, x=9).<br /><br />Ternary Operator<br /><br />Sometimes conditional operator ?: is also called a ternary operator. This is so because it involves three operands. For example:<br /><br /> x ? y : z<br /><br /><br />Here, x,y and z are the three operands. If condition x is true, then value y is assigned otherwise value z is assigned.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-19838814067890993762012-04-12T03:15:56.044-06:002012-04-12T03:15:56.044-06:00This is a short way of writting IF sentences. It i...This is a short way of writting IF sentences. It is also used in other languages like Java, Javascript and others.<br /><br />Your code:<br /><br />$protocol = $request_type == 'SSL' ? HTTPS_SERVER : HTTP_SERVER;<br /><br /><br />can be written like this:<br /><br />if($request_type == 'SSL')<br /> $protocol = HTTPS_SERVER;<br />else<br /> $protocol = HTTP_SERVER;Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-40162663032548647122012-04-12T03:15:55.122-06:002012-04-12T03:15:55.122-06:00This is sometimes known as the ternary conditional...This is sometimes known as the ternary conditional operator. Ternary means that it has three arguments, as x ? y : z. Basically, it checks if x is true; if it is, then put y instead of this operation, otherwise z.<br /><br />$hello = $something ? "Yes, it's true" : "No, it's false";Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-21655612810215057272012-04-12T03:15:54.073-06:002012-04-12T03:15:54.073-06:00It's called a ternary operator. If the first e...It's called a ternary operator. If the first expression evaluates to true, HTTPS_SERVER is used, else HTTP_SERVER is chosen.<br /><br />It's basically a shorthand if statement, the above code could also be rewritten as follows:<br /><br />if ($request_type == 'SSL') {<br /> HTTPS_SERVER;<br />} else {<br /> HTTP_SERVER;<br />}Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-83572501693464008432012-04-12T03:15:53.163-06:002012-04-12T03:15:53.163-06:00This is the conditional operator.
$x ? $y : $z
...This is the conditional operator.<br /><br />$x ? $y : $z<br /><br /><br />means "if $x is true, then use $y; otherwise use $z".<br /><br />People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com