FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


What does the ? do in this line of code





Possum
What does the ? do in this line of code

Code:
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : ROOT_PATH . '/';


$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : ROOT_PATH . '/';

thx
Marcuzzo
It is a conditional operator, mostly used when there is only 1 statement that needs to be executed per condition

where you would normally write:
Code:

$test = 'some value';
if ( condition )
{
   $test = 'condition true';
}
else
{
    $test = 'condition false';
}


you could also do this:
Code:
$text = ( condition ) ? 'condition true' : 'condition false';


so:
$variable = ( condition ) ? [return this to $variable when condition is true] : [return this when condition is false] ;
Possum
THx..

http://davidwalsh.name/php-ternary-examples

Its like a short hand for php...
Peterssidan
A lot of other languages also have this operator. It's often called the ternary operator. Ternary just means it takes 3 arguments but many languages don't have any other ternary operator so there is no confusion calling it the ternary operator.
jmraker
Possum wrote:
What does the ? do in this line of code

Code:
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : ROOT_PATH . '/';


$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : ROOT_PATH . '/';

thx

The statement says:
If the constant PHPBB_ROOT_PATH is defined the variable $phpbb_root_path is set to the value of the constant, otherwise it's set to ROOT_PATH with '/' appended to it. It isn't 100% obvious that '/' is only appended to ROOT_PATH, I had to test if it was inside or outside the last part of the statement. You probably need to really know your order precedence to reliably add these to complex statements.

The ":" in the statement goes with the "?" operator.
Peterssidan
You can always add a pair of parentheses to make more clear.
Code:
$phpbb_root_path = (defined('PHPBB_ROOT_PATH') ? PHPBB_ROOT_PATH : ROOT_PATH) . '/';
Marcuzzo
Peterssidan wrote:
You can always add a pair of parentheses to make more clear.
Code:
$phpbb_root_path = (defined('PHPBB_ROOT_PATH') ? PHPBB_ROOT_PATH : ROOT_PATH) . '/';


you missed one there Razz
Peterssidan
Marcuzzo wrote:
Peterssidan wrote:
You can always add a pair of parentheses to make more clear.
Code:
$phpbb_root_path = (defined('PHPBB_ROOT_PATH') ? PHPBB_ROOT_PATH : ROOT_PATH) . '/';

you missed one there Razz

I did remove the parentheses around defined('PHPBB_ROOT_PATH') because it was unnecessary and I find it harder to read with too many parentheses. What I wrote should work.
Possum
Works

Thx everyone.. I just xxx php

Code:
<?php
$var = 10;
$var_is_greater_than_two = $var > 20 ? "Cat" : "Dog"; // returns true
    echo "$var_is_greater_than_two";
?>


Works too

Code:
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
$var = 10;
$var_is_greater_than_two = ($var > 20 ? "Cat" : "Dog"); // returns true
    echo "$var_is_greater_than_two";
?>
</body>
</html>



Works

Code:
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
$var = 1000;
$var_is_greater_than_two = ($var > 200) ? "Cat" : "Dog"; // returns true
    echo "$var_is_greater_than_two";
?>
</body>
</html>
codersfriend
ternary operators are great. They make coding shorter and it is very useful for if statements that don't require a lot of else statements
Related topics
Why does this code make undeletable files?
Help: SQL error after uploading new files(thread updated)
How To : Improve Your PHP Programming
problem with attached css style sheet
Page File problem with FireFox
AJAX tutorial [2nd part now updated]
How can i make anonymous need to registered to before...
Help! I need a quick fix!
Session Expire
Windows is freer than Linux
Multiple File Extentions in C#
In Command: Syntax Highlighting your code
Firefox Links
Coding ~ Poetry ?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.