Saturday, October 10, 2015

PHP Basic Tutorial 1

Short description of PHP

PHP is server-side
Your browser doesn't realise the pages it is viewing are initially written with PHP. All it receives is an HTML page - as complex or as simple as you want.

PHP is HTML-embedded:
A PHP page can be simply an HTML page with a little PHP sprinkled here and there (we'll see how).
The PHP bits are parsed ("translated") by the server - in the examples presented here, they will be mainly used to produce more HTML code. The HTML code on the page is sent directly to the browser.

PHP has similarities with other programming languages:
C and Perl are two of them. In my case, learning a bit of Perl really helped me get started and understand what PHP could do for me - and how it worked.

Inserting PHP code in HTML

T
o insert bits of PHP code in an HTML page, all you have to do is insert it in a special tag, like this:
<? any amount of PHP code ?>

Note: depending on your HTML editor, or if you are using XML, you might need to use one of these alternate "PHP tags":


<?php "blah blah blah" ?>


<% "blah blah blah" %>

Here is an example which uses a variable:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <title> <? print($pagetitle); ?> </title>
</head>
<body>
HTML code... <? PHP code ?> ...HTML code... <? PHP code ?> ...HTML code... etc...
</body>
</html> /p>

The print statement in the "Title" line simply sends the value of the variable $pagetitle to the browser.
Let's say we had previously assigned a value to $pagetitle, by using the following expression (somewhere in the beginning of the same page, for example):
<? $pagetitle='This is my page!'; ?>
Then what the browser would receive is:
<html>
<head>
  <title>This is my page!</title>
</head>
etc...

As you can see, as far as the browser is concerned, this is just HTML...

Here is another example, using an include:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <title>This is my page!</title>
</head>
<body>
HTML code... <? require("nav.inc"); ?> ...HTML code... <? PHP code ?> ...HTML code... etc...
</body>
</html>

The require expression tells the server to stick in the contents of the file named "nav.inc", instead of the PHP tag. Let's say this file contains the main navigation table for your site and looks like this:
this is the beginning of the file 


<table class="nav">
  <tr> 
    <td><a href="/" title="Home page">Home</a></td> 
    <td><a href="/computers/" title="Everything about computers">Computers</a></td> 
    ... 
    <td><a href="/recipes/" title="My favorite recipes">Recipes</a></td> 
  </tr> 
</table> 



the file ends here

This is what the server sends to the browser:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <title>This is my page!</title>
</head>
<body>
HTML code... 


<table class="nav">
  <tr> 
    <td><a href="/" title="Home page">Home</a></td> 
    <td><a href="/computers/" title="Everything about computers">Computers</a></td> 
    ... 
    <td><a href="/recipes/" title="My favorite recipes">Recipes</a></td> 
  </tr> 
</table> 



...more HTML code... <? PHP code ?> ...HTML code... etc... 
</body>
</html>

Again, the browser has no idea that PHP exists.
Comments on the HTML code for the navigation table:
  • The class attribute tells the browser to apply the previously defined style sheet class named "nav" to the table. If the browser doesn't support CSS, it simply ignores it. You might want to know more about CSS.
  • I have used server-relative urls (beginning with a forward slash "/") and haven't specified the full path, as the link points to the index file in each directory.
  • The "title" attribute allows you to give more information about the link; some browsers may display it when the mouse goes over the link.

A little PHP syntax

Here is a little information about what matters and what doesn't when writing PHP.
  • Whitespace (tabs, spaces, returns) doesn't really matter. You can write <?$title="mytitle";?> or <? $title = "mytitle" ; ?>, both work.
  • Each statement ends with a semi-colon ";".
  • When inside PHP tags, you can add comments:
    • /* comment */
    • # comment
    • // comment
    (everything until the end of the line will be ignored in the two last cases; in the first case, the comment can span multiple lines).
  • You can switch from PHP to HTML and back just about anywhere. Make your experiences!
  • PHP files are usually named "something.php" (on some servers you can use "something.php3" or even "something.phtml").

PHP.net is your friend!

PHP.net can be overwhelming for the beginner. I spent hours getting lost in that site before I found out how to use it efficiently. Though the interface seems to be a little more friendly than it used to be, here are a few tips which might come in handy:
  • check out the introductory tutorial.
  • if you need information on a function, simply type php.net/function_name into your browser bar. Try php.net/str_replace or php.net/explode to get started.
  • if you want information about anything, typing php.net/whatever will open a search page for "whatever".

More about variables, print and echo

Variables can have just about any size. For example, all the text you are reading now is the value of one big variable named $content.
Variable names start with a dollar sign ("$"), followed by an alphabetic character or an underscore, optionally followed by alphanumeric characters or underscores.
They are case-sensitive. They do not have to be declared or assigned a type.
PHP also supports array variables (e.g. $somevar[3] = "something") and objects, but their discussion is beyond the scope of this article.

print and echo do just about the same thing: they send their argument to the browser. There is a small difference between them, but I'm not yet sure what it is... ; )
When using printecho, or when assigning a string value to a variable, you have the choice between using double quotes or single quotes (if the argument is a naked variable, you can also use no quotes at all). 

Single quotes will reproduce the text between them with no modifications. Line breaks, spacings, variable names and fun characters will all come out how you printed them. But! if the text between the quotes contains single quotes, you have to replace them by the escape sequence "backslash-quote": "\'". And what if you want a backslash? Use a double backslash: "\\". That's all. 
Double quotes will replace variables by their value, and ignore newlines and tabs. Here are some escape sequences: newline = "\n", tab = "\t", double quote = "\"", backslash = "\\", dollar = "$"... 

Until you know better, I recommend using single quotes unless the string contains a variable to be replaced.

If you want to define a very long variable (for example, lots of text), you might want to use the here doc syntax.
An empty variable, e.g. $potato = ''; will return false when its boolean value is called for. It will not create an error if you attempt to print it.

No comments:

Post a Comment