Here is a very short introduction to the principles of PHP. PHP is an acronym for “hypertext preprocessor”. PHP is a serverside language. We use PHP in order to prepare webpages, react to user input – and (on the third semester) to interact with databases.
Many Open Source CMSs use a combination of Apache, PHP and MySQL as an “engine”.
How to write PHP code
A php file is made in a manner similar to a HTML file. You create a file and give it the surname .php. So a file name sample could be:
- myFile.php
The file can contain other forms of code and tags such as JavaScript, CSS or HTML. In the file the php code is nested between a <? and ends with a ?>.
Variables, Strings and numbers
<?php
$a = 10; // a number
$b = “Hello World”; // a string
$c = “Ho, ho, hooo!”; // yes it’s another string
$d = “Santa Claus, says: “; // xmas is near
$combinedStrings = $d . $c; // Santa Claus, says: Ho, ho, hooo!
$e = NULL; // no value set
$f = true; // or false
?>
A variable is defined via the $ character. In the sample code above you can see several variables.
A string starts and ends with a quotation mark as ” or ‘. If you need quotation marks inside a string they should be nested like this:
<? $foo = “<img src=’image.png’ alt=’an image’ />”; ?>
You can create HTML code via PHP – and you can show an image on a web page like this:
<? echo $foo; ?>
or
<? print(“$foo”); ?>
Numbers
Since a variable can be a number, you can use PHP for calculations, such as:
<?
$x = 1;
$y = 19;
echo $x + $y; // should return 20
?>
See this page for more information about PHP and calculations.
Conditions
Arrays
Loops
Includes
<?
include(“myFile.php”);
require(“myFile.php”);
require_once(“myFile.php”);
?>
Leave a Reply