This is not going to be a tutorial which is going to explain in much detail about each and everything about PHP. My main aim is to make a tutorial which is easy to understand for someone who has experience with any of the programming language and now wants to get into the beautiful world of PHP.
PHP training by Swift Brains.
Installing the system to get PHP moving.
Windows:
Download WAMP and install it on your windows machine. It will install Apache webserver, MySQL, and PHP and would configure it such a way that all of them work together. Typical installation creates a www folder in C Drive.
Linux:
Download XAMPP and install it on your Linux machine.(I am assuming that you know how to install applications on Linux). This is also on the same lines as WAMP.
These tools are more so easy to use, yet if you feel any problem please feel free to contact me.
As an IDE (Integrated Development Environment) you can use Netbeans for PHP or anything you wish.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Lets start on with actual tutorial.
First PHP Script
<?PHP print("Hello World!!") ?>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHP Tag Starts and ends with either
<?PHP and ?> This is the best way to code
OR
<? and ?> Not recommended as you will have to tweak your php.ini file to get this working. This type of code is known as PHP short Tags. Tweak php.ini to set short_open_tag = On
OR
<% and %> ASP Style tags. Again you need to tweak php.ini to get this working. Tweak php.ini to set asp_tags = On;
OR
<SCRIPT LANGUAGE="PHP"> and </SCRIPT> you can also use this.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHP Script including HTML
Create a file, say, helloworld.php and put the below mentioned code in it. Place this file at appropriate place to run and then open your browser http://localhost/helloworld.php
<html>
<head>
<title> PHP Tutorial by Swift Brains </title>
</head>
<body>
<?php Print ("hello world"); ?>
</body>
</html>
PLEASE NOTE THAT A PHP STATEMENT ENDS WITH A SEMI COLON ";". IF YOU WILL NOT PUT IT THEN IT WON'T FUNCTION.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
How to add comments to PHP code
Single Line comments have // or # as the start
Multiple line comments are between /* and */
Examples:
// this is a comment
# this is another comment
/*
this is a comment
and its not read by PHP Interpreter
*
/
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Variables in PHP
Variables are container which can hold value.
Variables in PHP start with $ sign. Example: $variable1
Variables can contain numbers, letters, and underscore
Declaring PHP variable
Usually PHP Variables are declared and assigned value in the same statement
$var1 = 8
$var2 = 16
This means that if you write
<?php print 8; ?>
or
<?php print $var1; ?>
both would yield same result.
Dynamic Variables
PHP has a brilliant and at the same time quite confusing concept of Dynamic Variables.
$var1 = "value1";
this is same as
$holder = "var1";
$$holder = "value1";
The $holder variable contains the string "value1", so you can think of $$holder as a dollar sign followed by the value of $holder. PHP interprets this as $var1.
(Make it a habit to put semi colons are statement completion else you would not be able to get anything working)
Syntax to acess Dynamic Variables is exactly same.
As per above example
<?php print $var1; ?>
will print value1
but
<?php print $$holder; ?>
will not print value1 as a result, instead it will print $var1
Try with
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = "value1";
$holder = "var1";
Print ("$var1");
?>
</body>
</html>
AND
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = "value1";
$holder = "var1";
Print ("$$holder");
?>
</body>
</html>
Inorder to get value1 as the output for the second code also you need to write it this way
Print ("${$holder}") ;
Example Code:
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = "value1";
$holder = "var1";
Print ("${$holder}") ;
?>
</body>
</html>
References to Variables
By Value
You can provide reference to variables too.
$var1 = $var2;
This means that if $var2 had value 10 then after this statement $var1 will contain the value 10.
Now even if we change the value of $var2 to 20 still $var1 will contain the same old value 10.
By reference
this is done by use of & sign as shown in statement below.
$var1 = &$var2
Now if we change the value of any of them, the value of both the variables will change because right now both are accessing the same memoery location. This is same concept as in C pointer.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Data Types
Integer - 5
Double - 1.234
String - "hello world"
Boolean - true
Object - Discussed later
Array - discussed later
You can use PHP's inbuilt function gettype() to test the type of any variable. If you place a variable between the parentheses of the function call, gettype() returns a string representing the relevant type.
Example:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php
$var1 = "value1";
$var2 = 5;
print gettype($var1);
print gettype($var2);
?>
</body>
</html>
Returns
StringInteger i.e. var1 is string type and var2 is integer type.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Changing Variable Types
We can use settype() function to change the type of a variable.
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = "value1";
$var2 = 5;
settype($var2, string);
print gettype($var1);
print "<br>";
print gettype($var2);
?>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Type Casting
$var1 = (double) $var2;
This makes the var1 type as double.
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = "value1";
$var2 = 5;
settype($var1, integer);
$var2 = (double)$var1;
print gettype($var1);
print "<br>";
print gettype($var2);
?>
</body>
</html>
This would print
integer
double
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Operators and Expressions
Asignment Operator
$var1 = "value1";
print ($var1);
This results in value1
(I hope by now you are a little easy with programming in PHP so I am not writing the complete code everytime)
You can also do assignment as
print ($var1 = "value1");
This results in value1
Arithmetic Operators
+, -, *, /, %
% is a mod operator and gives output as the remainder. Example 10%3 gives result as 1.
Concatenation Operators
"hello"."world"
gives value as helloworld.
Regardless of datatype of the operands, they are treated as strings and the result is always a string.
Other Assignment Operators
$x = 4;
$x += 4; // $x now equals 8
is equivalent to
$x = 4;
$x = $x + 4; // $x now equals 8
So options are
+=
*=
-=
/=
%=
.=
Comparison Operators
== equivalance, left is equivalent to right
!= non equivalence, left is not equivalent right
=== identical, left is equivalent to right and both are of same type
> greater than
>= greater than equal to
< less than
<= less than equal to
