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


? In PHP Code

Posted on 2007-10-11
Medium Priority
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;
Question by:nplib

Expert Comment

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

Otherwise use it in regular expression
LVL 24

Accepted Solution

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;
   $php = $jim;
LVL 24

Expert Comment

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

LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20056908
I do not know that syntax, but I know the    ?  :  syntax:

$var = <expression> ? <value if expression is true> : <value if expression is not true> ;
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';


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