• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

? In PHP Code

I've been looking around and I can't find why would you use a "?" in your PHP code.

E.G.   $php = $x?$bob;
0
nplib
Asked:
nplib
1 Solution
 
siva_sivaCommented:
For query string only we use ? symbol.

Otherwise use it in regular expression
0
 
glcumminsCommented:
This is the same as :

if ($x)
{
  $php = $bob;
}

It is a short-hand method of creating an if-then structure. A more complete example is:

 $php = $x ? $bob : $jim;

This translates to:

 if ($x)
 {
   $php = $bob;
 }
 else
 {
   $php = $jim;
 }
0
 
glcumminsCommented:
For more information, take a look at the "Ternary Operator" section on this page:

 http://www.php.net/operators.comparison
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I do not know that syntax, but I know the    ?  :  syntax:
http://lu.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

$var = <expression> ? <value if expression is true> : <value if expression is not true> ;
0
 
Richard QuadlingSenior Software DeveloperCommented:
if (test() == 'something') {
 $res = 'left'
} else {
 $res = 'right';
}

can be written as ...

$res = test() == 'something' ? 'left' : 'right';

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now