PHP Basics

In the core alone of PHP are dozens and dozens of constructs, keywords, and functions. There are many hundreds of fascinating functions in the many dozen special-purpose modules that have been contributed and become accepted as standard extensions of the language. We cannot study these in depth here, but we will learn about some of them as they are encountered .


A PHP file is an HTML file. Look at our first exercise in PHP:

 <html> <head>  <title>First PHP Page</title></head> <body>If this seems like plain old HTML to you...<br/> you are right.</body> </html> 

Its unexciting output:


PHP becomes more than HTML when we actually introduce PHP script into the file. PHP script can be embedded anywhere in the HTML (and in fact can replace all the HTML). It is delimited by any of these equivalent options:

 <script language="PHP"> code   </script> <?php                   code         ?> <?                      code         ?> 

PHP is designed to make it easy to respond to a browser. The commands print() and echo both send text back to the browser. The differences? Print is a function and follows the usual pattern of enclosing its argument in parentheses. Echo is a keyword in the PHP language (like return) and it does not require parentheses. (Many people are more comfortable using the parentheses anyway, and some of our examples use them. But not this example.) Also, print() returns a boolean success value ( true is good), and print is buffered output while echo is not.

 <html> <head>  <title>Second PHP Page</title></head> <body>If this seems like plain old HTML,<br/> you are <? echo "wrong." ?> </body></html> 


PHP can do a lot more than echo string literals back to the browser. It's core language is rich and full. It is not unlike ActionScript ”with a few more constructs, a bit more depth, and a little less discipline.

Like most languages since von Neumann, PHP has words to describe data and to describe the things you want to do to it. Data is simple. Variables are recognized by the first character in their name , which is always $ . They do not have to be declared and they cannot be typed (except in the case of compound types like arrays and objects).

 <html> <head>  <title>Third PHP Page</title></head> <body> <? $right = "wrong"; echo "If this seems like plain old HTML,<br/> you are $right" ?> </body></html> 

The output of this is

 If this seems like plain old HTML, you are wrong 

PHP's minimalist variable types, string, int, float, and boolean, are distinguished casually by context when they are distinguished at all. The compound variable types, array and object, are explicitly declared. Arrays are extremely capable data types with a rich set of methods . Objects are less mature and (as in ActionScript) fall far short of the level of object-oriented implementation seen in Java or C++.

String Operators

The concatenation operator is the dot: "bath"."tub" resolves to "bathtub"; .= is a valid construct.

Strings within double quotes are evaluated. (Although the depth of evaluation is not specified, our experience leads us to trust PHP to resolve first-order names in this context but little else.)

 $suffix = "room"; echo "bath".$suffix; echo "bath$suffix"; 

returns this to the browser:

 bathroom bathroom 

Single quotes create string literals with precisely the same contents.

 $suffix = "room"; echo 'Try "bath$suffix".'; 

produces this output:

 Try "bath$suffix". 

This is sometimes useful in composing strings for HTML or for SQL where embedded quoted strings need to be literally preserved. However, we often find that this a clumsy approach when variables need to be resolved at the same time. When we compose SQL queries, we use escape sequences instead.

Flash and XML[c] A Developer[ap]s Guide
Flash and XML[c] A Developer[ap]s Guide
ISBN: 201729202
Year: 2005
Pages: 160 © 2008-2017.
If you may any questions please contact us: