chapter1PHPTUT Nicephotog
chapter1PHPTUT Nicephotog
Since this site is hosted for the promotion of PHP i have decided to include a" tutorial" on PHP aside to javascripting.
My actual site is at www.nicephotog-jvm.net
To be able to understand and utilise this tutorial you will require some comprehension of web sites file system naming and addressing both internally and on
the www,also, a good knowledge of HTML tagging mark-up language. If you know another script language to any usable level, that also will help.
PHP is a script language.Script languages pass their data to a run time interpreter/compiler and are written in text but not compiled to instructions or byte
code command binaries. The interpreter/run time compiler loads and reads the script that contains the instruction sequence language text and executes it with
the script engine binaries.
I first wrote a PHP script for my site as a search engine to see its compatibility with the PERL programming language. I found that it only quite stacks up in
PERL to say it is compatible but then it wouldn't be a promotion of itself if it was. Largely its compatibility usefulness difference is regular expression
construction being not completely supported but it does make a similar language at many levels that PERL has.
Unlike PERL, PHP is a much more deliberate Web Oriented language as its complete name suggests. It handles many functions for web servers in pre-defined
functions that are simply not present in the PERL language and are also simply a system hack to produce an equivalent.
This tutorial is a simple introduction to the common and basic activities in PHP and not directly to produce any proficiency, also i need to clean up my
knowledge of PHP because i use a large selection of languages for programming and the list of new technologies on the market is growing of number.
One final thing to do is to download yourself a PHP Language Manual from PHP.NET
This tutorial uses 4 , but there could be version 5 information added for some purposes.
Be careful to read the versioning for each of the functions in the php manual.
(Save As ZIP) THE CODE PRESENTED IN THIS TUTORIAL
<?php
?>
OR
#!/usr/bin/php/php
To start the actual program structure, all the required "global" variables will be declared at
the top before any other program parts. Variables are a reference(memory) for values fed to
the script (usually, and most are simply data) to be operated on. Other referencing actions can
be made the same way for program parts(and are not data/return data) called an Object but
are too complicated to explain at this point not that remembering something isn't.
e.g.
<?php
# This symbol starting the line to the left is known as the sharp symbol and all following
# on this line is a comment that will not be/cannot be used in the program.
$email_address = "[email protected]";
There are four things to recognise about the above line of code.
4. Finally the end is a semi colon ; indicating the code sequence is syntactically complete and
that instructions of assigning the value to the variable is finished and will be a start point after
for the next operation.
With this above code we can now put it into a special web server HTML file that is signified by its file
extension .php and will appear something like this.
<?php
$email_address = "[email protected]";
print($email_address);
?>
Note the new code that is the last line.The print() function is a pre-defined function that is contained
in the PHP language to send data to Standard Output. In this action of its use the file called
ouput-email-address.php would be placed into the root or otherwise called www dirctory of the server site
and called by a web browser causing the script to be loaded and run by the servers PHP engine. The output
from above would simply be in the top left corner of the browser [email protected] .
That is not particularly useful at this point but now we will look at why and how the PHP language is
used for web servers and special .php pages by introducing some simple standard html combined to the php.
Make a file called standard-html-template.php and put into it the next following code as
instructed.
$page_title="my pages title"; # the words my pages title can be changed to any text you want
To be sensible here it is best now to explain some of the syntax in this much more complicated string data
assignment to the variable called $html_head_begin that contains some 3 mysterious symbols/symbol
sequences.
1. This sequence of "." double quote followed by a dot then a double quote is a concatenation of the
two ends of strings. The dot is an operator in the php language (for now) called the string concatenator.
2. The string with the internal "\n" backslash and lower-case n is a symbol meaning that the PHP engine
is to output everything it reads after on a new(next) text line.
3. The third is a little less simple. Inside the html tag(<meta>) being assigned as a string is this sequence
<meta name=\"generator\" that contains \" a backslash and a double quote inside the string. This is
because in PHP the apostrophe or double quote is used to symbolise to the PHP engine at run time script read
the declaration/assignment of start and end of a string(text data). It needs some way of knowing the text from
the operator symbol so it is given a signifier backslash before it inside a string. If it is not present it is
likely the PHP engine will throw an error because it cannot find the correct end of string. While there are
exceptions to that rule it is not useful at this time to explain the exceptions. The action of backslashing a
special character is called escaping and the backslash is an escape(the escape operator). The escape
operator is also a symbol that can be read in text but to produce it in a script requires itself to be escaped
like this \\
<?php
################# standard-html-template.php #################
$page_title="my pages title"; # the words my pages title can be changed to any text you want
$html_head_begin = "<html>"."\n"."<br>"."<head>"."\n"."<br>"."<meta name=\"generator\" content=\"nicephot.xlphp.net php tutorial\">"."<title>";
print($html_head_begin.$page_title);
# next will be the print function to end the head tag and start the viewable page body of this .php file
?>
Notice above that the two variables have had the dot operator/string concatenation operator applied to them
inside the print() function. That action on variables can only be done to a pair of variables that contain text
data or data that can be represented as text that is contained in a variable. Often though it is required to use
the cast() function on numbers to produce it as text suitable for the PHP engine to output. The action of
assuming that (e.g a number) data will be changed to text automatically in a dot operation is called implicit casting.
PHP has dynamic data typing meaning it does not require declaring a variables data type before assignment, it
will simply be changed to it at run time as required.
Now make the head tag end and start of the visible page body with a positional alignment of the page content
in the centre with this...
print('</title></head><body><center><p>'."\n"."<br>");
The print() function above this time is not using variables but bare immediately unassigned strings. Notice
also that to produce a text new line always requires to use double quotes around it an not apostrophes(called
single quotes some-times).
Now your file should appear like this below and we will add some visible content to the page but it is not ready
to test run. Look below the ?> end of script marker...
<?php
################# standard-html-template.php #################
$page_title="my pages title"; # the words my pages title can be changed to any text you want
$html_head_begin = "<html>"."\n"."<br>"."<head>"."\n"."<br>"."<meta name=\"generator\" content=\"nicephot.xlphp.net php tutorial\">"."<title>";
print($html_head_begin.$page_title);
# next will be the print function to end the head tag and start the viewable page body of this .php file
print('</title></head><body><center><p>'."\n"."<br>");
?>
Here is some test text from the tutorial script
<br>
at nicephot.xlphp.net called standard-html-template.php
<p>
<?php
?>
Above the script we are writing to output from the web server is finished and following it is bare text html
tagging as would be in a normal html file. Also we are about to do another script after it. The script we will put
in after only contains the end html tags to complete the file as syntactically correct.
NOTE:Before the script is run after placing it in the web server root of your site it is best to change the
permission for the file to execute to 775 or 755 as suitable. Usually for PHP if it is set up it will not be required
to do any more than place it in the server and call the .php page.
Now look below at the script and your file should appear like this below and can be saved then uploaded and called.
When the script is called it should print two lines centralised in the page and it should show the page title in the
browsers strip across its window top. When you view the page source in the browser editor after calling the page its
code will only show the html that was output.
<?php
################# standard-html-template.php #################
$page_title="my pages title"; # the words my pages title can be changed to any text you want
$html_head_begin = "<html>"."\n"."<br>"."<head>"."\n"."<br>"."<meta name=\"generator\" content=\"nicephot.xlphp.net php tutorial\">"."<title>";
print($html_head_begin.$page_title);
# next will be the print function to end the head tag and start the viewable page body of this .php file
print('</title></head><body><center><p>'."\n"."<br>");
?>
Here is some test text from the PHP tutorial script
<br>
at nicephot.xlphp.net called standard-html-template.php
<?php
print('</p></center></body></html>');
?>