Jump to content

Recommended Posts

the above code is same as

 

if(isset($_POST['testbox']))
{
return $_POST['testbox'];
}else
{
return null;
}

 

if the code behind ? , returns true then $textbox is set to wat comes just after ? , if it return false then $textbox is set to wat comes after : .

if(isset($_POST['testbox'])) $testbox = $_POST['testbox'];

else $testbox = null;

 

Perhaps I asked my question incorrectly. 

 

I understand how the code functions, but I've never seen the "?" or ":" defined or even used before.  What's the purpose of the if, else statement if we have "?" and ":" to use.  Additionally, I've learned whatever I know from Php books, yet have never seen these two operators offered as an alternative to the "if else" statement.  Is there a reason for this?

It is called the Ternary Operator. Which as stated, it acts as a shortened if / else.

 

As for why it exists, it provides it uses. Namely, for the code you shown, which coincidently I wrote, allows for you to handle invalid / not passed in variables a bit easier without having to do a full on if statement. Some people prefer to use the full on if statement, but for simple assignments, why not use ternary? It does not detract from the code and a full on if statement is not usually needed as all you want to do is make sure that variable has a value assigned, namely for error testing purposes.

 

$name = isset($_POST['name'])?$_POST['name']:null;
$address = isset($_POST['address'])?$_POST['address']:null;

// VS: 
if (isset($_POST['name'])) 
    $name = $_POST['name'];
else
    $name = null;
if (isset($_POST['address']))
    $address = $_POST['address'];
else
    $address = null;

 

As stated, some people do not like it. I like it for assignments like the above, as it just makes sense to me to do it as such.

it should be used when it has to be used thats all.

 

It's not a oh I'll use this instread of if statements..

 

this is for shorting code that usually ends with a return call like fixing little things like money tax

 

$moneytax = ($money > 400) ? $money * 10 : 0;

shit like that but you see it always returns data.. thats why there is always a equals sign before it starts..

 

if statements don't return anything and in a if statment you'll have to declare $moneytax  2 times!

 

blah yah it doesnt make anything faster it makes code easier to read imo.

@r4nd

yup return shd be replaced with the var name

 

One particular use of this snippet comes handy with phtmls , where you are coding view part of MVC ,

 

<?=  isset($this->memberName) ? $this->memberName : 'Welcome Guest'; ?>

 

 

Word is that its not good to use <?= and <?, you should always use <?php.

I use template parser if I want to take advantage of View part in MVC.

 

Because only then HTML and PHP are truely separated.

@r4nd

yup return shd be replaced with the var name

 

One particular use of this snippet comes handy with phtmls , where you are coding view part of MVC ,

 

<?=  isset($this->memberName) ? $this->memberName : 'Welcome Guest'; ?>

 

 

That depends on how you model your application. I, for one always have a User object with the default name being Guest and rights set accordingly. So I would just write:

 

<?php echo 'Welcome, ', $this->memberName; ?>

@r4nd

yup return shd be replaced with the var name

 

One particular use of this snippet comes handy with phtmls , where you are coding view part of MVC ,

 

<?=  isset($this->memberName) ? $this->memberName : 'Welcome Guest'; ?>

 

 

Word is that its not good to use <?= and <?, you should always use <?php.

 

It's perfectly fine using those if you have control over the environment(s) that your PHP script will run it.

It's perfectly fine using those if you have control over the environment(s) that your PHP script will run it.

 

In case you don't understand by what he means with environment(s): you have access to the php.ini to configure allow_short_tags. However for portability and ease it's best to use <?php as this is guaranteed to always work.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.