Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Beginning PHP Programming, Part II
By Jeff Cogswell

Rate This Article: Add This Article To:

Beginning PHP Programming, Part II
( Page 1 of 3 )

Jeff Cogswell continues his introduction to the PHP programming language. In this week's PHP tutorial, Jeff talks more about program control as well as the details of server-side programming.

This week we continue our introduction to the PHP programming language. Today I’ll explain a bit more about program control, and then remind you about some of the details about web servers and programs that run on the server-side.

Understanding Program Control

In my previous article introducing PHP, I showed how you can put PHP code right in your HTML file. The trick is to make sure you have a .php filename extension, and that your PHP code is embedded inside a <?php ?> tag. Here’s a quick example from the previous article:

<html>
<body>
<h1>Hello 
<?php echo("world"); ?>
</h1>
</body>
</html>

The PHP processor will replace the <?php ?> tags with the output from the php code they contain. But the processor is incredibly smart and a bit more sophisticated than that. Look closely at the following code. (But note that it won’t run yet—we have to do a few things to it to make it function correctly. I’ll show you those shortly.)

<?php
if ($dayname == "Wednesday") {
    echo("Welcome to midweek!");
}
else {
    echo("Welcome!");
}
?>

This code is simply an if-then-else block. The if statement checks what day today is. If it’s Wednesday, then it prints out the message “Welcome to midweek!”. Otherwise it just prints the message “Welcome!

In the resulting HTML file, this entire block of PHP code will be replaced either by Welcome to midweek! or by Welcome!, depending on what day you access the page in your browser.

But what if you wanted to put the words in italic, with the word midweek in bold? Then you could do this:

<?php
if ($dayname == "Wednesday") {
    echo("<i>Welcome to <b>midweek</b>!</i>");
}
else {
    echo("<i>Welcome!</i>");
}
?>

In other words, you can put HTML tags right inside the strings that are printing out. Thus, the PHP code will either be replaced by

<i>Welcome to <b>midweek</b>!</i>

or by:

<i>Welcome!</i>

depending on the day the page is accessed in the browser. Then the browser will happily read the HTML tags for italic and bold, and display the message formatted appropriately.

But there’s actually another way to make this work instead of putting the HTML inside quoted strings and using the echo function. You can momentarily break out of the php tag, and use straight HTML like so:

<?php
if ($dayname == "Wednesday") {
?>
    <i>Welcome to <b>midweek</b>!</i>
<?php 
}
else {
?>
    <i>Welcome!</i>
<?php 
}
?>

This code looks a little messier, but it actually gives you more control over the output of your HTML. Look at the first three lines; they’re just an if line surrounded by a php tag. The next lines isn’t inside a php tag. It’s just HTML. But this HTML will only appear in the resulting HTML file if the if test works, that is, if today is Wednesday. Notice the lines that follow; they make up the closing brace of the then block of the if statement, the else keyword, and the opening brace of the else block:

<?php 
}
else {
?>

Then again we have a line of straight HTML code, followed by a php tag containing the closing brace for the else block. This second straight HTML code will only appear if the test isn’t true, that is, if today is not Wednesday.

Thus, we have these two blocks of pure HTML:

<i>Welcome to <b>midweek</b>!</i>

and

<i>Welcome!</i>

The first one will appear in the final file if today is Wednesday. Otherwise the second one will.

Ready to try this out? You can’t yet until you add a couple more lines to get today’s date. Let’s do that next.



 
 
>>> More Microsoft Languages Articles          >>> More By Jeff Cogswell