Page 1 of 1

? & : in PHP

Posted: Fri Feb 20, 2004 1:57 am
by evilcoder
hey guys, i use them alot, but only by recognition that i need them not because i know what they do or mean:


Code: Select all

<?php
$subject = ( !empty($subject) ) ? preg_replace($orig_word, $replace_word, $subject) : '';
?>
What do the "?" and ":" mean in this example?

Thanks.

Posted: Fri Feb 20, 2004 2:16 am
by Dr Evil
This is short syntax for if...else.

Code: Select all

<?php
$subject = ( !empty($subject) ) ? preg_replace($orig_word, $replace_word, $subject) : ''; 

//is equal but shorter to

if (!empty($subject){
$subject=preg_replace($orig_word, $replace_word, $subject)
}
else{
// do nothing
}

?>

Posted: Fri Feb 20, 2004 7:21 am
by evilcoder
Is that all? nothing else?

Posted: Fri Feb 20, 2004 8:05 am
by Dr Evil
Well it's a nice way of making things more compact. But that is all I know.
:roll:

Were you expecting anymore ?

Posted: Fri Feb 20, 2004 9:28 am
by pickle
evilcoder wrote:Is that all? nothing else?
Yep, that's all they are.

Posted: Fri Feb 20, 2004 7:47 pm
by evilcoder
no thats cool. Just wondering, thanks heaps for replying, appreciate it alot. :)

Have a good day guys.