Archive for the ‘php’ Category

An intro to php

Introduction

  • The PHP programming language is a server-side HTML embedded scripting language.
  • PHP stands for PHP: Hypertext Preprocessor.PHP is free and is an open source software product.
  • The PHP scripts are executed on the server.
    PHP supports many databases (MySQL, Sybase, Oracle and many others.)
  • PHP runs on different platforms (Unix, Linux, Windows.)
    PHP is compatible with almost all web-servers used today (Apache, IIS, etc.)
  • A PHP file can contain plain text, HTML tags and scripts
    The PHP files can have one of the following extensions: php, php3 or phtml.

Basic Syntax

Its nothing but simple html, inside the body PHP scripting block is placed beginning with <?php and ending with ?>. The only new thing is the ‘echo’ statement. The echo function will print everything on the screen that is placed between “”
<html>
<head>
</head>
<body>
<?php echo “Hello World, this is my first PHP program!” ?>
</body>
</html>

This code will print Hello World, this is my first PHP program!

Variable declaration

<?php
$my_variable = 20;
$my_variable2 = “Hello World”;
?>

As in C,C# etc. string functions,arithmetic, assignment, logical operations all can be performed.

The Control Statements, if, else if, while, do, for loop all have similar structure as that in other languages like C,C#.

Quick demos can be illustrated as:

if – else block

<?php
$var=1;
if($var == 2) {
echo “The if statement is true”;
} else {
echo “The if statement is not true”;
}
?>

for loop

<?php
for ($x=0; $x<=5; $x++)
{
echo “Number is ” . $x . “<br />”;
}
?>

while loop

<?php
$x=0;
while($x<=5)
{
echo “The number is ” . $x . “<br />”;
$x++;
}
?>

Functions

Pretty much similar execution and logic when compared to other languages 🙂

html>
<body>

<?php
function myFunction()
{
echo “black”;
}

echo “My car has the color “;
myFunction();
?>

</body>
</html>

output – My car has the color black

Functions with arguments can be illustrated as :

<?php
function ourFunction($x)
{
echo $x . “.<br />”;
}

$y = “black”;
echo “My car color is “;
ourFunction(“white”);

echo “My car color is “;
ourFunction (“$y”);
?>

Output will be like:
My car color is white.

My car color is black.

 

include and require functions – As name suggests, include will include the specified php file while execution and require refers that, in order the code to execute,it requires the specified file, if the specified file doesn’t exist, the execution will break.

Simple example will make it clear 🙂

Let the header.php be a file  having a simple echo statement “yahoo”

<html>
<body>

<?php include(“header.php”); ?>
<p>Some example text!</p>

</body>
</html>

Output of this code will be like:

yahoo

Some example text!

where as if header.php file doesn’t exist,

<html>
<body>

<?php
require(“header.php”);
echo “Just some random text!”;
?>

</body>
</html>

Will result in error and the execution will be stopped and hence the echo statement is never executed.

 

This is just a basic intro to php language 🙂

Tag Cloud