? & : in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

? & : in PHP

Post 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.
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post 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
}

?>
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

Is that all? nothing else?
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post 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 ?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

evilcoder wrote:Is that all? nothing else?
Yep, that's all they are.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

no thats cool. Just wondering, thanks heaps for replying, appreciate it alot. :)

Have a good day guys.
Post Reply