home news blogs forums events research newsletter whitepapers careers


Network Computing Network Computing Network Computing
HOT PICKS

IMMERSE YOURSELF:

SOA

  |

Data Center

  |

802.11n

  |

Data Privacy

  |
APO  |

Virtualization

  |

NAC

  |

Security

  |

Network Mgmt

  |

Enterprise Apps

  |

Storage & Servers





Chapter 2: Beginning PHP 4

December 18, 2000

Brought to you by:




Table of contents:
The Different Types of Code

As we said before this example, we are aiming first and foremost to demonstrate the three different types of code used on this page. So let's look at the code we just typed in, and assign it into each of the three categories:

<HTML>
<BODY>
The date at the moment is
<?php echo gmdate("M d Y");
?>
</BODY>
</HTML>

The code with the clear background isn't really code at all; it's just plain text. There's really nothing more to say about it.

The code with the light gray background is HTML markup. Markup of any type is easy to spot because it's denoted by opening and closing angled brackets known as tags. It isn't really programming code in the traditional sense, as you will see, hence we will use the term markup to describe our HTML tags. You should be familiar with HTML so we're not going to discuss it much further here.

One thing to note though is that we're going to capitalize the HTML tags throughout this book, despite the fact that strictly speaking it's against the standards (the HTML 4.0 standard suggests that all HTML tags should be in lower case). The reason for this is that we don't want to continue using the awkward color scheme shown above throughout the book, and all browsers are case-insensitive (they don't care whether the tags are in upper or lower case) anyway.

Bright sparks who know about XHTML, will know that this language is a rendering of the HTML 4.01 standard (the most recent standard in XML), and will also probably know that XHTML is intolerant of a lot of aspects of HTML that browsers tend to ignore. What this means is that XHTML will not allow you to get away with using obsolete tags, or without closing tags properly, and it also requires HTML to be in lower case. As yet, there are no XHTML-only browsers in mainstream use, so for this reason we're not going to worry about the upper case.

The code with the dark gray background is PHP script. The PHP script denoted by angled brackets, and question marks as well. So, anytime you see a portion of code that starts with <?php and ?> tags you know that everything inside must be PHP script.

As you can see from the results output to the browser, the three types of content quite happily co-exist in the web page, despite the fact that the PHP script must be processed by the web server (which may be on a different machine from your browser).

How the Code Works

Having established the nature of the different types of code on the page, we've so far neglected to explain what the one line of PHP code in the program does. So let's rectify that now - the only actual PHP script in the page was the following:

echo gmdate("M d Y");

Three things are going on here. Firstly, the word echo() is a PHP command, which takes whatever is fed into it and displays it on the web page. So if we had the line:

echo "Hello world";

you would see the words Hello world on the screen when you browsed that web page. However, you may have noticed that in our example the date appeared on the screen, yet we only fed the function gmdate("M d Y") to the echo() command - this is the second thing that is going on, and we'll look at it in a moment.

PHP has a large library of these reserved words, known as functions, and they perform popular tasks such as returning the date or time, sending e-mail, executing some of the more complex mathematical operations, or pausing the execution of a script for a few seconds. There is a list of all the functions in Appendix D. You don't need to know these off by heart, but you will find that time and time again throughout the book we will be making use of them.

The final issue is the semi-colon; PHP requires that every line of code (there are one or two exceptions to this rule as we shall see), when completed, should be terminated with a semi-colon to denote where that particular line ends.

You might have noticed that the word gmdate() wasn't enclosed by quotation marks. In our "Hello world" example, by enclosing the text we want to display in quotation marks, we are telling the PHP engine that we want to display everything between the quotation marks exactly as written. In our case, when we don't include quotation marks, we are telling the engine to use a special function of PHP's called gmdate() to run an operation for us. The function retrieves today's date and time in Greenwich Mean Time. Let's discuss how gmdate() works in greater detail.

You may have noticed that in our example we displayed the date as follows:

Aug 15 2000

However, there are many different formats for displaying today's date. For example, you could have:

15/8/00 09-30AM

or

Tuesday 15th August

These are both valid formats of today's date, yet PHP has no idea which version you want it to use, or whether you want it to use an entirely different version altogether. So, you need to tell it what to do, and that's exactly what the section in parentheses and quotation marks after gmdate() does. We specify that we want the month first, followed by the day and then the year. The fact that we use upper case and lower case D's, M's, and Y's is also significant. PHP attaches different meanings to each as shown in the table that follows:


As you can see, this is quite a comprehensive list. Once again, we're not going to reproduce a table every time we want to outline the workings of a new PHP function, so you will need to refer to the appendices. Equally, we could have changed our previous example to:

echo gmDate("D");

This would display:

Tue

Alternatively, we could have changed it to:

echo gmDate("d m Y");

We leave this last example as an exercise to the reader to workout what will be displayed. Now we have a better understanding of what the code is actually doing, we can move on...




PAGE: 1 I 2 I 3 I 4 I NEXT PAGE
 





Ready to take that job and shove it?

Function:

Keyword(s):

State:
SPONSOR
RECENT JOB POSTINGS
CAREER NEWS
Go beyond Google and get vertical. These specialized search sites will help you find the business information you need -- fast.

Ari Balogh was named to the post of chief technology officer as the companys for a "realignment" of employees.










InformationWeek U.S. IT Salary Survey 2008
Salaries for business technology professionals are falling. Here's what you need to know in order to make good hiring decisions and personal career choices. Purchase Today: $299
 
ROLLING RIGHT ALONG
Follow key Network Computing Reviews from conception to completion. This Week: Holistic APM.



Network Computing Reports Emerging Enterprise Podcast Series: Secrets to Success








TechSearch


Microsite of the Week


Powerful Information at Your Fingertips



techweb
Online Communities TechWebInformationWeekLight ReadingIntelligent EnterprisebMightyNetwork ComputingDark ReadingDigital LibraryWall Street & Technology
Byte & SwitchNo JitterInternet EvolutionLight Reading's Cable Digital NewsContentinopleUnStrungBank Systems & TechnologyAdvanced TradingInsurance & Technology
Face-to-Face Events
InteropWeb 2.0 ExpoWeb 2.0 SummitVoiceConBlack HatCSISoftwareEntrprise 2.0 ConferenceGTEC
Mobile Business Expo
InformationWeek 500 ConferenceBuy Side Trading XchangeBuy Side Trading SummitBank Executive SummitInsurance Executive SummitTelcoTVEthernet ExpoOptical Expo
Magazines  
InformationWeekWall Street & TechnologyInsurance & TechnologyBank Systems & TechnologyAdvanced TradingMSDNTechNetSmart EnterpriseThe Architecture JournalDatabase Magazine
 
Research & Analyst Services  
Heavy ReadingInformationWeek ReportsInformationWeek Analytics
 
   
   
App Infrastructure   |   Messaging & Collaboration   |   Network & Systems Mgmt   |   Network Infrastructure   |   Security  |   Storage & Servers   |   Wireless   |   Enterprise Apps
About Us  |  Contact Us  |  Site Map  |  Technology Marketing Solutions  |   Briefing Centers
Copyright © 2008  United Business Media Limited  |  Privacy Statement  |  Terms of Service  |  Your California Privacy Rights