You are on page 1of 20

PHP Basics

PHP is a programming language that was designed for creating


dynamic websites.
It slots into your web server and processes instructions contained in
a web page before that page is sent through to your web browser
The name PHP is a recursive acronym that stands for PHP:
Hypertext Preprocessor. It began life called PHP/FI, the "FI" part
standing for Forms Interpreter. Then the name was shortened a
while back.
one of PHP's most powerful features is how easy it becomes to
process data submitted in HTML forms. PHP can also talk to various
database systems, giving you the ability to generate a web page
based on a SQL query.
The PHP language is flexible and fairly forgiving,
making it easy to learn even if you have not done any
programming in the past.
PHP looks like a cross between C, Perl, and Java, and if
you are familiar with any of these, you will find that
you can adapt your existing programming style to PHP
with little effort.
PHP Tags
Consider the following extract from a PHP-driven web
page that displays the current date:
Today is <?php echo date('j F Y');?>
The <?php tag tells PHP that everything that follows is
program code rather than HTML, until the closing ?> tag.
In this example, the echo command tells PHP to display
the next item to screen; the following date command
produces a formatted version of the current date,
containing the day, month, and year.
Displaying the System Date and Time
The time is <?php echo date('H:i:s');?>
and the date is <?php echo date('j F Y');?>
The Echo Command
While PHP is great for embedding small, dynamic
elements inside a web page, in fact the whole page
could consist of a set of PHP instructions to generate
the output if the entire script were enclosed in PHP
tags.
The echo command is used to send output to the
browser.
Using echo to Send Output to the Browser
<?php echo "The time is ";
echo date('H:i:s');
echo " and the date is ";
echo date('j F Y'); ?>
Comments
Another way to make sure your code remains readable
is by adding comments to it. A comment is a piece of
free text that can appear anywhere in a script and is
completely ignored by PHP.
The different comment styles supported by PHP are
shown below
Comments(con)
Using Comments in a Script
<?php
/* time.php
This script prints the current date
and time in the web browser
*/

echo "The time is ";


echo date('H:i:s'); // Hours, minutes, seconds

echo " and the date is ";


echo date('j F Y'); // Day name, month name, year
?>
Variables
Variables are not only containers in which values can
be stored and later retrieved but also and a fundamental
building blocks of any programming language.
For instance, you could have a variable called number
that holds the value 5 or a variable called name that
holds the value Abdi. The following PHP code declares
variables with those names and values:
$number = 5;
$name = “Abdi";
Variables(con)
In PHP, a variable name is always prefixed with a dollar sign.
If you remember that, declaring a new variable is very easy: You
just use an equals symbol with the variable name on the left and
the value you want it to take on the right.
Declaring Variables : Unlike in some programming languages, in
PHP variables do not need to be declared before they can be
used. You can assign a value to a new variable name any time you
want to start using it.
Variables can be used in place of fixed values throughout the
PHP language.
Naming Variables
The more descriptive your variable names are, the more easily you will
remember what they are used for when you come back to a script several
months after you write it.
It is not usually a good idea to call your variables $a, $b, and so on.
You probably won't remember what each letter stood for, if anything, for
long.
Good variable names tell exactly what kind of value you can expect to find
stored in them (for example, $price or $name).
Case-Sensitivity : Variable names in PHP are case-sensitive.
 For example, $name is a different variable than $Name, and the two could
store different values in the same script.
Using Underscores Using the underscore character is a handy
way to give a variable a name that is made up of two or more
words. For example $first_name and $date_of_birth are more
readable for having underscores in place.
Another popular convention for combining words is to
capitalize the first letter of each word for example,
$FirstName and $DateOfBirth. If you prefer this style, feel
free to use it in your scripts but remember that the
capitalization does matter.
Expressions
When a variable assignment takes place, the value
given does not have to be a fixed value.
It could be an expression two or more values
combined using an operator to produce a result.
It should be fairly obvious how the following example
works, but the following text breaks it down into its
components:
$sum = 16 + 30;
echo $sum;
Variables in Strings
You have already seen that text strings need to be enclosed in
quotation marks and learned that there is a difference between
single and double quotes.
The difference is that a dollar sign in a double-quoted string
indicates that the current value of that variable should become part
of the string. In a single-quoted string, on the other hand, the dollar
sign is treated as a literal character, and no reference is made to any
variables.
The following examples should help explain this. In the following
example, the value of variable $name is included in the string:
$name = ‘Abdi';
echo 'Hello, $name';
Data Types
Every variable that holds a value also has a data type
that defines what kind of value it is holding. The basic
data types in PHP are shown below
Data Types(con)
When you assign a value to a variable, the data type of the
variable is also set.
PHP determines the data type automatically, based on the
value you assign.
If you want to check what data type PHP thinks a value is,
you can use the gettype function.
Running the following code shows that the data type of a
decimal number is double:
$value = 7.2;
echo gettype($value);
 The complementary function to gettype is settype, which allows you to
override the data type of a variable. If the stored value is not suitable to
be stored in the new type, it will be modified to the closest value
possible.
 The following code attempts to convert a string value into an integer:
$value = "22nd January 2005";
settype($value, "integer");
echo $value;

 Analyzing Data Types : In practice, you will not use settype and gettype
very often because you will rarely need to alter the data type of a
variable.
Variable Variables
It is possible to use the value stored in a variable as the
name of another variable. If this sounds confusing, the
following example might help:
$my_age = 21;
$varname = "my_age";
echo "The value of $varname is ${$varname}";

You might also like