[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

? In PHP Code

Posted on 2007-10-11
5
Medium Priority
?
246 Views
Last Modified: 2007-10-11
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
Comment
Question by:nplib
5 Comments
 
LVL 5

Expert Comment

by:siva_siva
ID: 20056838
For query string only we use ? symbol.

Otherwise use it in regular expression
0
 
LVL 24

Accepted Solution

by:
glcummins earned 200 total points
ID: 20056841
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
 
LVL 24

Expert Comment

by:glcummins
ID: 20056862
For more information, take a look at the "Ternary Operator" section on this page:

 http://www.php.net/operators.comparison
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20056908
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
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20057070
if (test() == 'something') {
 $res = 'left'
} else {
 $res = 'right';
}

can be written as ...

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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 10 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question