What is the difference between the two If's


I have used this line of code many times before:

<h2><?=isset($getItems['name']) ? 'Edit' : 'Add'?> items</h2>

Now i have recently started using an MVC structure php 5. To do this i have used netbeans instead of Web Expression which i normally use.

Problem is that that line of code above doesn’t work and i am having to change it to this:

<h2><?php if(isset($getItems['name'])){ echo 'Edit'; } else { echo 'Add'; } ?> items</h2>

Why is this? I prefer the first method, the second method doesn’t seem the best way?? How can i get the first method to work?

Any ideas?


The first way works fine, but php needs to be configured to use short_open_tag. So, be aware different php installations may not have it enabled, so you might not want to depend on it if you don’t have control over it(with typical shared or managed hosting, you may not).

Even without that php config setting enabled, you can still

<h2><?php echo isset($getItems['name']) ? 'Edit' : 'Add'; ?> items</h2>


Worked perfectly :slight_smile:

The one difference if you enable shorttags is that the doctype tag gets mixed up with the short tag that starts PHP and so you have to separate the first two and last two characters in order for it to be able to process the doctype properly.