Category Archives: PHP

Modular code in PHP

Modular code in PHP

Modular code

Modular code is a small piece of code that perform separate task and we can use again and again, this type of code is called modular code. Pearl package is the good example of modular code. There are many pearl packages in word that have thousands of modular code.

Why used modular code?

Code reusability: – The main advantage of modular code is code reusability, write once use many times in all application and you can also make code library and use this modular code in lot of application by including this library.
If you want to learn how include function works visit include funtion.

Ease of maintenance: – Suppose you have ten copy of same code in ten different places of your application and you want to fix bugs or enhance the functionality of code then you have to change this code in all page. If you are using modular code you can change one place and all places code has changed.

Readability: – You can increase the efficiency of your program by using modular code. If you divide your program into lots of modular code then obviously readability increases.

Efficiency: – Modular code increase the efficiency of you application. Suppose you have big PHP file that have a lot of PHP code and you want to access only array sorting code. If you using only sorting of array code is more efficient than using hole code that written in a PHP file.

PHP contain a lot of feature to implement the modular code.

Function

You can write piece of code in a function and use again and again by calling the function in anywhere of our application. I already told you, you can make function library to use this modular code in another applications.

Syntax

function function_name()

{

Your code;

}

Example


<?php

function test()
{
echo "Hello, world! <br/> ";

echo "welcome to php";
}

test(); //you can call any number of times 
test(); // like that
test();

?>

Output

Hello, world!
welcome to phpHello, world!
welcome to phpHello, world!
welcome to php

Classes

Classes contain both property and function within a single class that also enhance the security of application.

Syntax of class

class class_name 

{
Property; 
Behaviour;
}

Example

<?php

class Car 
{
public $color;
public $manufacturer;
}
$first = new Car();
$first -> color = "blue";
$first -> manufacturer = "Audi";
$second = new Car();
$second -> color = "yellow";
$second -> manufacturer = "bmw";
echo " Some properties: ";
echo " <p> The Beetle's color is " . $first -> color . ". </p> ";
echo " <p> The Mustang's manufacturer is " . $second -> manufacturer . ". </p> ";
echo " The \$first Object: <pre> ";
print_r( $first );
echo " </pre> ";
echo " The \$second Object: <pre> ";
print_r( $second );
echo " </pre>";
?>

Output

Some properties:

The Beetle’s color is blue.

The Mustang’s manufacturer is bmw.

The $first Object:

Car Object
(
[color] => blue
[manufacturer] => Audi
)

The $second Object:

Car Object
(
[color] => yellow
[manufacturer] => bmw
)

Include () and require ()

You can include the piece of code from one script to another scrip by using these functions.

Namespaces

These are useful for large applications and libraries. They help to avoid clashes where the same class name, function name, or constant name is accidentally used in different libraries, or in different parts of an application

If you want to learn more about functions, classes, include, and function library please visit
php tutorial



PHP Here document

Here document |pTutorial

Here document use as a special input output redirection to feed a command list to an interactive program.

It is the way to large block of code into a single variable.

Syntax of here document

<?php
$variable_name=<<<EOF
Code here
-------------------
echo "php tutorial";
echo"www.ptutorial.com";
-------------------
-------------------
-------------------
-------------------
-------------------

EOF;
Echo $variable_name;
?>

Following two example explain the concept of here document

Simple example with a lot of echo statement

<?php

Echo” WWW.ptutorial.com”;
Echo “
<ul>”;
	Echo”<LI>PHP tutorial</li>
”;
	Echo”<LI>java tutorial</li>
”;
              Echo”<LI>C example</li>
”;
echo“</uL>”;
echo “
<h3>Created by ptutorial.com</h3>
”;

?>

Output

WWW.ptutorial.com

  • PHP tutorial
  • java tutorial
  • C example

Created by ptutorial.com

Example using here technique

<?php
$here_doc = <<<EOF
<h3> WWW.ptutorial.com</h3>
<ul>
	<LI>PHP tutorial</li>
<LI>java tutorial</li>
<LI>C example</li>
</uL>
<h3>Created by ptutorial.com</h3>
EOF;

echo ($here_doc);
?>

Output

WWW.ptutorial.com

PHP tutorial
java tutorial
C example
Created by ptutorial.com

EOF denote the start and end point of string. This can be any combination of alphanumeric character. All the rule of making variable are applied here.

If you want to learn more about PHP visit : Php tutorial



Php Explode Function

PHP 5 Converting Arrays into Strings |pTutorial

Many times you want to perform manipulation on data but the manipulation require different format of data.

For example you have line of string and you want to access particular word. PHP allows you to create an array that contains every element in separate array element.

Explode function

You can convert string into the array with the help of explode function in the following format:

$variable_array = explode(“seprator”,any string);

Let’s take example to explain the concept of explode

<?php

$variable_string = "Sometimes students simply run out of times.";

$variable_array = explode(" ", $variable_string);

print_r($variable_array);

?>

 

Output

Array ( [0] => Sometimes [1] => students [2] => simply [3] => run [4] => out [5] => of [6] => times. )

Note: The string variable is not affected.

Another example of explode function

<?php

$variable_string = "Sometimes students simply run out of times.";$variable_array = explode(" ", 

$variable_string);foreach($variable_array as $value)
{
echo $value."" ;
}

?>

Output

Sometimes
students
simply
run
out
of
times.

 

Let’s take one more example to elaborate the concept of explode function

<?php

$variable_string = "Sometimes,students,simply,run,out,of,times.";$variable_array = explode(",", 

$variable_string);foreach($variable_array as $value)
{
echo $value."" ;
}

<?php

 

Output

Sometimes
students
simply
run
out
of
times.

$variable_array is a simple array variable, so you can manipulate as simple as array variable. You can also access the particular element of the array, length of array. You can perform all operation that you perform on array.

Example

<?php

$variable_string = "Sometimes students simply run out of times.";

$variable_array = explode(" ", $variable_string);

echo "3rd element of array:"; // simply

echo $variable_array[2];

<?php

Output

3rd element of array:simply

If you want to learn more function of PHP visit :Php function