PHP if else statement

I'm working on PHP and I wanted to try and verbalize what's going on. I appreciate corrections. I also have questionS.

The if statement is saying if 'MSIE' is found within the variable the condition is true and it writes that you are using IE.

I've read that !== sets the condition to false if the criteria (MSIE) is not found. What specifically does !== mean? Is there a scenario where you would set that to true or is that script just stating something that's implied?

My other question is why do you have <?php ?> multiple times. Why wouldn't you just enclose the whole code with one?

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
?>
<h3>strpos() must have returned non-false</h3>
<p>You are using Internet Explorer</p>
<?php
} else {
?>
<h3>strpos() must have returned false</h3>
<p>You are not using Internet Explorer</p>
<?php
}
?>
burnedfacelessAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
!== means "not identical"

PHP is a loosely typed language, meaning that (among other things) zero, NULL and empty strings all take the same meaning as FALSE.

Does that help?
0
Ray PaseurCommented:
This may be a good learning resource for you.
http://php.net/tut.php

See the warning box here:
http://php.net/manual/en/function.strpos.php

The comparison table is here:
http://php.net/manual/en/language.operators.comparison.php
0
burnedfacelessAuthor Commented:
I'm about to read what you posted. I'll give you credit after I do.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Marco GasiFreelancerCommented:
!== and === mean 'not identical' and 'identical' respectively, whereas !? and == mean 'not equal' and 'equal' respectively (see here: http://it2.php.net/manual/en/language.operators.comparison.php)

For your second question, in your sample php code is merged with html code: you could use that tags only once, but you should use echo any line and you would miss the help of your code editor in highlighting correspondent tags:

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
echo "<h3>strpos() must have returned non-false</h3>
<p>You are using Internet Explorer</p>";
} else {
echo "<h3>strpos() must have returned false</h3>
<p>You are not using Internet Explorer</p>";
}
?>

This way, your code editor doesn't would recognize html code and it would not highlight it: html code would be treated as a normal string.

Hope this helps.

Cheers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
burnedfacelessAuthor Commented:
That was helpful information. I bookmarked the pages to read more in depth.

I have one more question. Why is <?php    ?> used multiple times? Is it because there is HTML after the PHP? In other words to let the server know that what follows is php? I guess a better question is when do you need to put <?php ?>?
0
Marco GasiFreelancerCommented:
Consider also that === and !== are reccomended for some function such as file_put_contents() (http://it2.php.net/file_put_contents): as you read in piunk warning, these functions "may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function."

Cheers
0
Marco GasiFreelancerCommented:
I explained that in my comment ID:37234450
0
Sudaraka WijesingheWeb Application ProgrammerCommented:
To add some thing to Ray's comment:

"not identical" means it checks both value and type of the two operands.
This is useful in cases like checking the return value of strpos where it can return either an integer or a boolean value.

For the second part:
PHP interpreter only process the code inside the <?php ?> tags. Although you can place your static HTML output in echo statements and have everything in a one PHP open/close tags, placing them out of the scope of PHP interpreter optimizes the execution.
0
Dave BaldwinFixer of ProblemsCommented:
Below is a PHP page that will show you all the SERVER variables that your server supports.  Different web servers support a few different variables.  This page http://www.useragentstring.com/pages/useragentstring.php lists many of the user agents from different browsers and robots.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PHP Server Variables</title>
</head>
<body><h1>PHP Server Variables</h1>
<?php

foreach($_SERVER as $key => $value) {
    echo "<b>$key :</b> $value<br />\n";
}

?>
</body>
</html>

Open in new window

0
burnedfacelessAuthor Commented:
Thanks. It's starting to look like you can do a lot with php.
0
Ray PaseurCommented:
I've been away to church for a few hours, so I missed some of this dialog.  But there is something here I want to pick up on.

Why is <?php    ?> used multiple times?

What a great leading question!  

Here is a rather longwinded answer, but I hope it helps you get off to a good start in your programming.  With PHP you can intermix logic and presentation.  You can start generating some HTML, then flip over to PHP logic, then back to HTML as many times as you want to do it.  And therein lies one of the greatest risks in developing software with PHP.  It's subtle and if you're the only programmer working on a project you may never have a problem because of it, but as systems become more complex and more people try to work together that "ease-of-use" feature can create nightmares.  Here is the sort of thing that happens.  You look at a web page and see a widget or a feature you like.  So you start looking into the code to see where PHP generated the HTML.  And you find that it is in the middle of creating the output page.  Since you like it, you decide to copy the code to another web page.  And the new page looks great so you put it up.  But later that day, for some reason, the new page suddenly does not work.  Moreover, there are strange things showing up in the data base.  Unwittingly, the PHP logic in the middle of the presentation layer overwrote a variable that the other page depended on, and the cascade of bugs began.  The site goes down, your boss calls you on Sunday afternoon with "Get in here now!"  If this were an edge case or an isolated instance I would not bother to mention it, but it happens more often than anyone in our industry cares to admit.

When PHP was invented (I've been using it since PHP 3) it was designed to be a language so easy to learn that "even your grandmother could build web sites."  And in the infancy of the web, it was a wonderfully easy thing, enabling all kinds of novices to begin creating web sites.  Somewhere along the way we went from the early investors like Amazon.com, who spent $500,000,000 to build the web site before they earned their first dime, to the notion that every kid with a PC and an internet connection can build web sites.  PHP played a large part in this democratization.  People with absolutely no computer science background could start building web sites!

And with widespread acceptance came widespread security catastrophes.  The quality of PHP code did not really matter to the owners of the new web companies; they just wanted to be the first to market.  PHP seemed like the way to go.  But some of the issues created by the "ease-of-use" motivation behind PHP were especially nasty.  Whole companies sprang up just for the purpose of refactoring PHP code that was written by inexperienced programmers.  Two articles that talk about some of the wrongheaded parts of PHP are here.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_6630-Magic-Quotes-a-bad-idea-from-day-one.html
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7317-Register-Globals-a-bad-idea-from-day-one.html

Today, PHP has an object oriented model that pretty much makes sense.  And you can use things like HEREDOC syntax to make your presentation layer more powerful and flexible.  But the central tenet remains, and that is "Separate your software into Model, View, Controller."  If you do that and make it an enduring habit you will become a proficient programmer very quickly.  And if you intermix the logic and presentation, you will probably be able to make simple sites run, but you will never get one of the leading jobs in the industry.  Some of those jobs are pretty good.  Digg is a PHP machine.  So are parts of Google, much of Yahoo, and almost all of Facebook.  You won't find very many <?php ?> constructs in their code.  The programmer's utopia would have only one <?php tag per script file.  (Sidebar note, the use of the close-PHP tag is to be avoided.)

Consider getting this book.  It goes way beyond basic syntax and parse errors, and brings up things like why you use certain design patterns.  It's been reprinted a few times, and never fails to get good reviews.
http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/dp/143022925X/

So can Grandma really use PHP to build web sites?  Maybe, if they are simple sites.  Grandma's pretty smart, but I do not think she has given much time to thinking about things like whether multiple inheritance is a good idea, or whether to use late static bindings.  For better or worse, PHP has matured far beyond its humble beginnings.  And even though there are still many novice programmers who will intermix logic and presentation (even some here at EE), there are PHP programmers today who understand the value of the MVC pattern and who follow generally accepted principles of computer science in the design of their programs.  That's the way the industry is going.

Best regards, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.