Functions in php

Functions:-

Functions is a block of code that is executed when we need to execute it.

Why we need functions:-

Functions are used whenever we want to use same block of code within our program whenever we need, instead of writing same code again and again it is better approach to make its function.

Types of functions:-

Normally there are two types of functions

  1. Builtin Functions
  2. Userdefined Functions

Builtin Functions:-

Some functions are already written and defined by programming language itself, means they are already built, just need to call them whenever u use it, these functions are called builtin functions. Like mysql_num_rows() is a builtin function that tell u about no of rows.

Userdefined Functions:-

Userdefined Functions are those functions that are defined and declared by programmer, means programmer write these functions whenever he wants. Below in examples we are dealing with Wserdefined Functions.

How to create functions in php?

  1. Function should start with the word function.
  2. Function name should be logical, means if you want to make function to add numbers, write its name add() or sum()

Function defining and function calling:-

Function defining is a process in which we define our function that is use later. To use a function in a program we just use the term calling a function. Whenever we want to use any function we just call it.

function consists of two parts

Example 1:- A simple function

function HelloWorld()

{

echo "Hello World";

}

How to call this function?

Lets make a php script and use this function

function Hello()
{
echo "Hello World";
}

echo "Just want to say
";

Hello();

Output:-

Just want to say

Hello World

Adding Parameters to the function:-

The above function Hello() is a very simple function because its only displaying static string. To make function more handy we can add parameters to the function. A parameter is worked like a variable. The parameters are used inside the function parentheses

Example 2- Function with parameters

function Hello($mystring)

{

echo $mystring;

}

echo "I just want to say
";

Hello("Hello php programmers");

Output:-

I just want to say

Hello php programmers

Note:- It's up to you, how many parameters you used in a function.

Get values return by function

Function can also return values, it can return nothing or some values, its up to their use.

Function dosum($a,$b)

{

$sum=$a+$b;

return $sum;

}

echo "sum is".dosum(1,2);

Output:-

sum is 3

Read more...

Arrays in php

Array in php is used to store similar set of data. Array is used to store one or more elements in a single variable name.

Each element in the array can be easily accessed through its index.

Types of Array
Normally there are three types of array used in the php.
1) Numeric Array.
2)Associative Array.
3)Multidimensional Array.

Numeric Array
Any element in numeric array is stored with a numeric ID key.

Example:-

$names_people = array("Adeel","Haris","Omer");

You can also assign the ID key yourself!

$names_people[0] = "Adeel";
$names_people[1] = "Haris";
$names_people[2] = "Omer";

Now you can get the values of array element like this, for example you want to the value of second element because Read more...

Loops in php

The term Loops is used when there is repetition involved in your php program. Means, you want to execute the same block of code to the number of times.

Normally php has four types of loop
  1. for loop
  2. while loop
  3. do while loop
  4. foreach loop

for loop:-

for loop is used when you know exactly how long you want to run the code segment, let's have a look over following program.

<?php
for($i=1;$i<6;$i++)

{

echo "Hello World";

}?>

Output will be:-

Hello WorldHello WorldHello WorldHello WorldHello World

Yeah this is exactly what we will get, now we want to show every Hello World in new line, for this you should use html br tag or \n Escape Sequence. For example

for($i=1;$i<6;$i++)
{
echo "Hello World\n";
}

Let's Look and Understand For Loop Structure

for (initialization; condition; increment)
{
code execution;
}

First of all we will initialize our variable, in the above case it is $i=1 , then condition is applied, if condition will true then body of loop will run, means then code within body of loop will be executed and loop gets increment of one, if condition is wrong then loop will be terminated and nothing happen.

Clear your concept

Many people thinks that loops get increment before code to be executed in the body of loop, its totally wrong.

Note:- It's not must that loop will be always incremented by 1 , its depend upon you, you can make it happen for loop to be incremented 2 or no of times what you want, if you want from loop to be incremented 2 times then use this

i+2 instead of i++ becuase i++ means i=i+1

While Loop

While loop is used when you dont know how many times loop will be executed, also loop will only run if condition will true. Let's look over while loop structure and a sample example.

Example:-

<?php
$i=1;

while($i<6)

{

echo "Hello World";

$i++;

}
?>

You will be saying in your heart that in this program of while loop we know that loop will be executed 5 times, yes you are write but normally while loop in php is used when we don't know exactly how many times code will run for example we want to run the code no of times, according to records in database, now we don.t know how many times the loop will run. The above illustrated example was just to tell you the basic structure of while loop.

Do - While Loop

do while loop is exactly same as while loop but in this loop code in the body of loop runs atleast one time, no matter whether condition is true or false. let's have a look over it.

<?php
$i=1;

do {

$i++;

echo "Hello World";

}

while ($i<6); ?>

As you see that body of loop is executed first then condition is checked so if condition is wrong then loop will run atleast one.

Note:- Do while loop is used rarely in php

foreach loop

The foreach loop is used to get values from arrays. For every loop, the value of the current array element is assigned to $val and the array pointer will be moved by one and so, on.

<?php

$arr_name=array("a", "b", "c");

foreach ($arr_name as $val)
{
echo "Value is: " . $val;
}


?> Read more...

if else condition in php

In this php tutorial i will discuss about use of if else condition in php. The syntax of if else condition in php is almost same with c language. We use if else statement when there is some sort of condition involve in php program. Code block is executed if condition is true and another code code block is executed for alternate situation.


if else condition in php

<?php

$name="Muhammad Ali";

if($name=="Muhammad Ali")
{
echo "Welcome Muhammad Ali";
}
else{
echo "Welcome User";
}
?>

Note:- == is called equal to operator that lies under logical operators category.


else if condition in php


Let's have a look over else if condition in php
<?php

$name="Muhammad Ali";

if($name=="Muhammad Ali")
{
echo "Welcome Muhammad Ali";
}
else if($name=="David")
{
echo "Welcome David";
}
else{
echo "Welcome Guest";
}
?>

I hope you have learned a lot about if else condition in php.
Read more...

Reference Variable in php

In this tutorial you will get the knowledge about reference variable. Any variable will be called reference variable, if it contains another variable as its value.


Reference variable in php

<?php
$a="b";

$b="a";

$c=$$b;

echo $c;

?>

Now its output will be b


How?
Let's solve it
  1. $c=$$b;
  2. $c=$a //Because $b value is a
  3. $c=b // Because $a value is b
So $c is a reference variable.
Read more...

use of variables in php

In this tutorial you will learn how to use variables in php. In php variables are always declared with $ (Dollar) sign, it means every thing in php is a variable that have $ sign.


Variable Declaration in PHP

Here is the Syntax to declare the variables in php.
$name="MUHAMMAD ALI";
$val=1;

There are three things that need to be explained in above code segment.
  1. Variable always decalre with $ sign preceding it.
  2. Strings are always quoted. (Strings are combination of Characters)
  3. Integers are never quoted.
  4. There must be semicolon (;) after variable declaration.
  5. You can use any name for variable but be logistic when you want to give names to variables.
Note:- It is not compulsory in php to declare the variables before using it.


How to print the value of variables in php?


There are different ways to print the value of variables in php.
<?php $name="Muhammad Ali";?>
print $name;

Output will be Muhammad Ali
echo $name;

Output will be Muhammad Ali
printf($name);

Output will be Muhammad Ali


Use of html tags in php
Case Senario:- You want to print the name that is in $name variable in bold, here is the solution.

<?php echo "<strong>".$name."</strong>";?>


Note:- . is concationation operator.

Another method for this is
<?php echo "<strong>$name</strong>"; ?>


Another method is

<strong> <?php echo $name; ?></strong>


I hope you will enjoy this tutorial of using variables in php
Read more...

Introduction to PHP

In this short but very meaningful tutorial i will tell you about PHP programming language that is being used widely for creating websites.PHP is a programming language use to create dynamic web application. PHP is a server side language, it means its scripts are executed on server.

Advantages of php over different server side languages .

  1. php is a open source language.
  2. php interacts with almost all databases, no matter whether you are using MS ACCESS, ORACLE, MYSQL or any other database.
  3. php can be used with any server, no matter whether you are using IIS or Apache.
  4. php is free to use and download.
  5. php Syntax is quite easy.
  6. php code runs and work effeciently on all platforms no matter whether it is windows, linux, unix etc.
PHP File
  1. php file extension is always .php
  2. php file can contain html code inside it.
  3. php file always return to the browser in the form of plain html since it is a server side language so its code is executed at server.
Read more...