Table of Contents
Basics:
PHP is unique in that it is a Hypertext Parser and not just a language. Hence PHP interfaces with HTML very very easily. In
fact it interfaces with HTML so easily that you can copy over any existing HTML page and change the extension to “.php” or “.cgi” and
it will still work. This is because PHP code itself only works within a certain kind of tag. For example:
1. 2. 3. 4. 5. 6. 7. //PHP code goes here 8. ?> 9. 10
As you can see in the above example, PHP code will only run inside the and ?> tags. Everything outside of those tags, PHP will simply be printed out. This allows for PHP to be incorporated in already existing webpages with great ease.
Commenting:
Because of CS50 (Introduction to Computer Science), the heavenly mandate that demands commenting has been drilled thoroughly into me. Thus, I find it imperative to explain commenting first. It is also one of the easiest topics to cover and necessary to many things.
If you were a careful reader you would have noticed that I used a comment in the above code. The commenting style in PHP is identical
to many other languages, namely C++ and Java. Hence, both // and /* */ works in the exact same way.
Another way to comment is with the # like in Perl or UNIX shell scripting. Here is an example of commenting:
1. <?php 2. // This is a Java/C++ style comment. 3. 4. /* This is also a comment in C style. 5. This is inside the comment block. */ 6. 7. # This is a Perl style comment. 8. ?>
One thing to keep in mind is this: Like many other programming languages, every non-comment line must end with a semicolon (;) !!! It is a stupid point if you know it already, but if you don’t you’ll be thoroughly confused by parser errors.
Variables:
In PHP, variables are handled in a very convenient manner. It is not like C or C++ where you have to worry alot about the datatypes of all of your variables and have to declare all of your variables. In PHP, variables are handled more like Java and exactly like Perl (except for one minor difference which will be explained later). Variables can be declared anywhere. You also do not have to worry about datatypes, as PHP will try to convert any datatype into anyother datatype when necessary.
Variables in PHP are demarked with a dollar ($). In other words, every variable must start with a $. This is what tells the PHP parser to consider the next word to be a variable.
Rather than try to explain all the things that you can do with variables, I will give you an example and show you how it works.
1. <?php 2. $n1 = 1; // The numerical value 1 => n1 3. $n2 = 3.5; // The numerical value 3.5 => n2 4. $a = 10; // 10 is stored to a 5. $b = “Hello world!!”; // “Hello world!!” => b 6. $c = $b.” Hi!”; // “Hello world!! Hi!” 7. // is stored to c 8. $d = “Ten is “.$a; // “Ten is 10″ => d 9. $e = “Ten is $a.”; // “Ten is 10.” => e 10 ?>
There are basically only two datatypes that one will really needs to be worried about: Numerical datatypes and Strings. Numerical
datatypes are simply numbers including int and float. Strings are anything enclosed within a set of quotes. Lines 1-5 in the above example show the setting of both numerical and string variables.
In Lines 6-8, a very strange thing is happening (if you don’t know Perl). Line 6 reads: $c = $b.” Hello again!”;. This line takes the value of $b and uses the dot operator (.) to concatenate the two strings together. In Line 8, something that is very different from C occurs: The numerical value 10 is automatically converted to a string datatype! Thus PHP handles the conversion of numerical datatypes to strings inherently.
In Line 9, the perferable method of inserting another variable’s
value into another string is used. Within the set of quotes, any
variable will be evaluated, and its value is converted and inserted
into the string. Hence the variable $e will have the value
“Ten is 10.” Note the period at the end of the string! The
equivalent, of course, could have been achieved in Line 8 with:
$d = “Ten is “.$a.”.”; But this is much harder to read than
Line 9.
Stylistic note: I prefer using the dot operator only when
using the return value from a function to incorporate into a string
(more on this later) and when concatenating the value of arrays
together. In all other cases, I usually evaluate them inside the
string. Because beauty is in the eye of the beholder, you are left
to your own discretion, but this is the convention that I will try
to follow from now forth.
One interesting feature of PHP that I had neglected to included but
was reminded by Blake Johnson is that there is a convenient way to print
variables inside HTML code. Here is an example:
1. 2. $n1 = 1; // The numerical value 1 => n1 3. ?> 4. 5.The value of $n1:
Using the synatx , we can immediately
insert the value of the variable into the HTML code.
Function Calling:
I won’t spend too much time on this, as it should be completely
self-evident if you know a bit about programming. Basically,
any word that does not have a special character in front of it will
be considered a function. The arguments following the command must
be enclosed in parenthesis. (There are a few exceptions, such as
print whose parenthesis are completely optional. However,
for the sake of consistency, it is nice to enclose the arguments to
any function with parenthesis. People who learned Perl first will
argue with me and will more than likely use the syntax that they are
most familiar with.)
So let us do some function calling.
1. 2. 3. print(”hello world “); 4. 5. $a = file(”temp.txt”); 6. 7. $b = “ < < < <”; 8. print(” “.htmlentities($a).” “); 9. 10 ?>
In this example, we find see the function print in Line
3 being called to print out HTML code. In Line 5, we find that the
function file is called, and the array that it returns is
stored into the variable $a (more on arrays later). In
Line 8, we find that the return value of the function
htmlentities is being concatenated with the dot operator to
become enclosed within a set of HTML tags.
So you may be asking yourself what exactly do these functions do?
This is beyond the scope of this tutorial. The aim of this tutorial
is to provide you with enough of the basics of PHP to begin coding
in it armed with only the PHP documentation. That said, I will show
you how you can use the documentation to understand how these
functions work.
- First go to the
PHP function reference. - Once there, you have a few options:
- In the upper left corner, there is a text field labeled
“lookup:” Here you can type the name of the function about which
you wish to know more. - If you are seeking for a function to perform a certain task,
then look through the Table of Contents to find the task that
you are interested in and view all the available functions
related to that task. For instance, the function
htmlentities can be found under “String Functions.”
- In the upper left corner, there is a text field labeled
- After finding the function that you are interested in, look at
the function description to understand how the function works and
more importantly see if the function is supported in the version and
compiled options of PHP that you have (see
Appendix for details)
Post A Comment