Home » Labs » Learn PHP – PHP basics and tutorial  

Learn PHP – PHP basics and tutorial


Filed under Labs

PHP is one of the most popular programming languages used on the internet for developing web applications. In this post we are sharing the basics of PHP including from Syntax, saving a PHP page, examples of PHP variables and usage and its naming rules etc. This tutorial is for those who are beginner s in PHP programming. By mastering the basics of PHP language you can build stunning web applications.

PHP Syntax Overview

The global effective PHP tag style is as below.

<?php
 
// your codes here
 
?>

how to save your PHP pages

All php files must save with .php extension instead of the standard .html extension.

Commenting PHP Code

Commenting in programming means making a portion readable only for human and it will not execute while running the program. This is very useful in advanced developing because it gives clear idea to others what are you aiming with codes. In PHP there are several ways of commenting , examples are as below.

<?php
// Single line comment
 
# Another method of making Single line comment

/*
This is a multiple lines comment block, 
you can make short notes here
*/
 
// You can also use comments to leave out parts of a code line
$x = 25 /* + 50 */ + 25;
echo $x;
?>

Example of combining PHP and HTML

PHP is an HTML-embedded server-side scripting language. Most of its syntax is borrowed from C. In web development you need to combine PHP and HTML and here is the example for doing it.

<html>
<head>
<title>Example Page</title>
</head>
<body>
	<?php
		echo "Hello World!";
	?>
</body>
</html>

PHP variables

A variable means holder of some kind of data, in PHP, a variable starts with the $ sign, followed by the name of the variable.
php variable naming rules
PHP variables must start with a letter or underscore “_”.
PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9
Variables with more than one word should be separated with underscores. $my_name

example of declaring PHP variable are as follows.

<?php
	$my_name = "My Name is John";
	$number = 10;
	$anotherNumber =5;
?>

PHP echo and print statements

You can output the results in PHP by using either echo or print statements. Both are almost same the difference is echo can take multiple parameters while print can take one argument.

 
<?php
	$data = "Hello World!";
	echo $data;
 
	echo "<h1>Hello World!</h1><br>";
 
	// using print Statement
 
	print "Hello world!";
?>

PHP strings

In PHP you can create a variable and store string directly to it.Example of creating strings in PHP are as below.

<?php
	$string_1 = "This is a string in double quotes";
	$string_0 = ""; // a string with zero characters
 
        echo $string_1;
 
?>

PHP integer

An integer is a whole number (without decimals). It is a number between -2,147,483,648 and +2,147,483,647.

Rules for integers

  • An integer must have at least one digit (0-9)
  • An integer cannot contain comma or blanks
  • An integer must not have a decimal point
  • An integer can be either positive or negative

Example of PHP Integer

 
<?php
	$int_var = 12345;
	$another_int = -12345 + 12345;
?>

PHP operators

Operators are used to manipulate or perform operations on variables and values. PHP operators are mainly classified as below.

  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • String Operators
  • Combination Arithmetic & Assignment Operators

PHP include and require statements

You can combine one PHP file into another PHP file with the include or require statement. Examples for include and require statements are below.

 
<?php 
	include("navigation.php"); 
?>
 
<?php
   $path = $_SERVER['DOCUMENT_ROOT'];
   $path .= "/common/header.php";
   include_once($path);
?>
 
<?php
	require 'navigation.php';
?>

PHP If…Else Statement

If….else statement in PHP is using for execute some code if a condition is true and another code if a condition is false.Example of PHP If…Else Statement is below.

 
	<?php
	$date=date("D");
		if ($date=="Fri")
		  echo "Have a nice weekend!"; 
		else
		  echo "Have a nice day!"; 
	?>

PHP Switch statement

If you want to select one of many blocks of code to be executed, use the Switch statement.

 
	<?php
		$date=date("D");
		switch ($date)
		{
		case "Mon":
		  echo "Today is Monday";
		  break;
		case "Tue":
		  echo "Today is Tuesday";
		  break;
		case "Wed":
		  echo "Today is Wednesday";
		  break;
		case "Thu":
		  echo "Today is Thursday";
		  break;
		case "Fri":
		  echo "Today is Friday";
		  break;
		case "Sat":
		  echo "Today is Saturday";
		  break;
		case "Sun":
		  echo "Today is Sunday";
		  break;
		default:
		  echo "No such day in calender";
		}
	?>

PHP for loop statement

Loops in PHP are used to execute the same block of code a specified number of times. PHP supports for, while, do…while and foreach loops. Example of for loop statement are below.

 
<?php
	$a = 0;
	$b = 0;
 
	for( $i=0; $i<5; $i++ )
	{
		$a += 10;
		$b += 5;
	}
	echo ("At the end of the loop a=$a and b=$b" );
?>

PHP while loop statement

The while statement will execute a block of code if and as long as a test expression is true.Below is an example of PHP while loop.

 
	<?php
		$i = 0;
		$num = 50;
 
		while( $i < 10)
		{
		   $num--;
		   $i++;
		}
		echo ("Loop stopped at i = $i and num = $num" );
	?>

PHP do…while loop statement

The do…while statement will execute a block of code at least once – it then will repeat the loop as long as a condition is true.

 
	<?php
		$i = 0;
		$num = 0;
		do
		{
		  $i++;
		}while( $i < 10 );
		echo ("Loop stopped at i = $i" );
	?>

PHP foreach loop statement

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

 
	<?php
		$array = array( 1, 2, 3, 4, 5);
		foreach( $array as $value )
		{
		  echo "Value is $value <br />";
		}
	?>

PHP break statement

The PHP break keyword is used to terminate the execution of a loop prematurely.

 
	<?php
		$i = 0;
 
		while( $i < 10)
		{
		   $i++;
		   if( $i == 3 )break;
		}
		echo ("Loop stopped at i = $i" );
	?>

PHP continue statement

The PHP continue keyword is used to halt the current iteration of a loop but it does not terminate the loop.

 
	<?php
		$array = array( 1, 2, 3, 4, 5);
		foreach( $array as $value )
		{
		  if( $value == 3 )continue;
		  echo "Value is $value <br />";
		}
	?>
It's only fair to share...Share on Google+Tweet about this on TwitterShare on Facebook

Posts you may like: