Jump to content

[SOLVED] very quick question


nikefido

Recommended Posts

can someone really quickly explain the use of "?" and ":" in a conditional statement?

 

ie. rather than using IF ELSE etc you do something like

 

$title ? dothis : orDoThis; //not sure if this is right

 

is this like typing:

 

if($title) {
doThis;
}else{
orDoThis;
}

 

??

Link to comment
Share on other sites

yes...the first part is your conditional statement, then after the ? is if it's true and after the : is if it's false. It's use most to shorten up this case:

 

<?php
if($test > 3){
  $result = 'first case';
}else{
  $result = 'second case';
}
?>

shortened to

<?php
$result = $test > 3 ? 'first case' : 'second case';
?>

Link to comment
Share on other sites

<test_expression>?<true_expression>:<false_expression>

 

U use boolean conditional when an expression is required.

$sample = <expression>;

 

just provides a shortcut when u have 2 results based on an expression, and returns either the true expression or false expression depending on the test_expression.

 

if($gender=='m') $display='Male';

else $display='Female';

 

see all the repetition

 

shorten with

$display=($gender=='m'?'Male':'Female')

 

 

 

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.