Archive

Category Archives for "Php & MySql"

php mysql tutorials, form basic concepts to advance topics and concepts.

2

How to Make String Palindrome in PHP

How to Make String Palindrome: A string is said to be Palindrome, if its reverse string is the same as it is!.

Let suppose we have a string = “TAT ” now if we reverse it !

it will still be TAT. so we simply say that the string is palindrome.Lets look at the simple example to understand all this.


<?php

$str = "tat";
$reverse_str= strrev($str);
if( $reverse_str == $str){

echo"the string ".$str."is Palindrome";
}
else
{
echo"the string ".$str."is not Palindrome";
}
?>

 

Now lets try the same Palindrome without using strrev() function.

Palindrome without using strrev() function in php

<?php

$str = “level”;
$str_array = str_split($str);
$str_reverse =””;
$size = count($str_array);
echo”the size of string is “.$size;

for($i=$size-1; $i>=0;$i–){

$str_reverse.=$str_array[$i];

}
if($str_reverse == $str){

echo “this is Palindrome”;}

else{
echo “this is not Palindrome”;
}

?>

Please do post your comments .

 

PHP Global Variables and HTML Form

PHP & MySQL Course: PHP Global Variables and HTML Form

In the previous lecture 11, we learnt about the advanced programming in PHP. One thing I should clear in this series of the course the advanced programming in PHP is not over yet. But in this lecture we will clear few topics which are most important to know about them.

Lecture 12:PHP Global Variables and HTML Form

In this lecture we will learn about the transferring of the data and their issues during the transfer of data. We will use three variables in PHP programming to transfer data from one webpage to another webpage. These two variables commonly called predefined variables as superglobal variables. And also we will use the HTML form tags to modify data. In this lecture will simply design a contact us webpage which is an important part for the commercial websites.

  1. HTML Form Tag
  2. $_POST variable
  3. $_GET variable
  4. $_REQUEST variable

So let’s we first of all define them what are they actually?

HTML Form Tag:

The HTML Form Tag is use to design a website with textboxes, comment boxes, buttons etc. we use <form> tag to start it and </form> to end the tag. And between them we use tags for textboxes, comment boxes etc. For detail lets we use it, check listing 1

Listing 1: HTML Form Tag


<html>

<head>

<title>Contact Us</title>

</head>

<body>

<h3>Contact-Us</h3>

<form>

<selection>

<option value="0"></option>

<option value="1">Mr.</option>

<option value="2">Miss.</option>

<option value="3">Mrs.</option>

</selection>

First Name: <input type="text" name="fname" /> <br>

Last Name:  <input type="text" name="lname" /> <br>

Email:              <input type="text" name="email" /> <br>

Message: <br><textarea type="text" name="message" col="100" rows="5" ></textarea> <br>

<input type="submit" vlaue="Submit" name="submit" />

</form>

</body>

</html>

Php Global variables

Figure 1  Form tag result as a contact us webpage

In the figure 1 you can see the output of the Listing 1. As I told you that using the form tag we can add buttons, text areas etc. The Listing 1 is just showing the pure HTML use of form, but we can design it in HTML 5.

HTTP Protocol:

Before we start to learn the predefined variable as they are superglobal variable too. But we should know about the HTTP. The HTTP is used to enable the communication between two things which are server and client. In the website the our application which is web browser is a client and we use the today’s lecture predefined variable to send communicate with server by sending data.

When we press the submit button the $_POST, $_GET, and $_REQUEST will send data to the server with help of HTTP. And then server will show the data as its reply.

$_POST:

$_POST actually is a variable of PHP, which is used for transferring data from one webpage to another webpages.  $_POST is a predefined variable and it is used to access data from forms when the submit button is pressed, then HTTP transfer the data to another webpage. And it is secure to transfer data from one webpage to another webpage. Lets we see how we can use the $_POST to submit data from one webpage to another webpage. Check the listing 2 for details

Listing 2: Using $_POST in PHP


<?

$FirstName = $_POST['fname'];

$LastName = $_POST['lname'];

$Email = $_POST['email'];

$Message = $_POST['message'];

echo "<b>Full Name is: </b>".$FirstName." ".$LastName."<br>";

echo "<b>Email Address is: </b>".$Email."<br>";

echo "<b>Message is:</b> ".$Message."<br>";

?>

$_GET:

This variable $_GET is same like $_POST but there are some difference between them. As we can bookmark the data from $_GET but we can’t do it from $_POST. The most important thing is that your data will be visible to everyone by using the $_GET but don’t if you use $_POST. $_GET is also an  superglobal variable which is commonly called predefined variable in PHP programming. Lets we see how we can use $_GET check the listing 3 for details.

Listing 3: Using $_GET in PHP


<?

$FirstName = $_GET['fname'];

$LastName = $_GET['lname'];

$Email = $_GET['email'];

$Message = $_GET['message'];

echo "<b>Full Name is: </b>".$FirstName." ".$LastName."<br>";

echo "<b>Email Address is: </b>".$Email."<br>";

echo "<b>Message is:</b> ".$Message."<br>";

?>

We just simple changed the program of Listing 2 in Listing 3’s program by replacing $_POST with $_GET, as you can see the difference between listing 2 and listing 3.

$_REQUEST:

If you don’t want to use the above two predefined variables so you can use the $_REQUEST place of them. There is a simple use of $_REQUEST built-in function, it is not a problem if we are using one of them from $_POST or $_GET in the form. $_REQUEST will perform both functionality, lets we define it in a PHP program. Just by replacing the $_POST and $_GET as we did in previous listings 2 from 1.

Listing 4: Using $_REQUEST in PHP


<?

$FirstName = $_REQUEST['fname'];

$LastName = $_REQUEST['lname'];

$Email = $_REQUEST['email'];

$Message = $_REQUEST['message'];

echo "<b>Full Name is: </b>".$FirstName." ".$LastName."<br>";

echo "<b>Email Address is: </b>".$Email."<br>";

echo "<b>Message is:</b> ".$Message."<br>";

?>

But one we didn’t do yet, that is to modify the form which we designed at the beginning of this lecture in the listing 1. For details check listing 5. In which we will use post keyword to send the request to the HTTP then the request will go to the server. Let’s see it

Listing 5: HTML Form Tag with Method POST


<html>

<head>

<title>Contact Us</title>

</head>

<body>

<h3>Contact-Us</h3>

<form action="contact-us.php" method="post">

<selection>

<option value="0"></option>

<option value="1">Mr.</option>

<option value="2">Miss.</option>

<option value="3">Mrs.</option>

</selection>

First Name: <input type="text" name="fname" /> <br>

Last Name:  <input type="text" name="lname" /> <br>

Email:                    <input type="text" name="email" /> <br>

Message: <br><textarea type="text" name="message" ></textarea> <br>

<input type="submit" vlaue="Submit" name="submit" />

</form>

</body>

</html>

As you can see the I have add a few things inside of the form tag that is method and action. If you are using the $_GET function in your PHP programming so you should just change the post in get from the method, but if you are using the $_REQUEST so you should not change anything from the form webpage.

Summary:

In this lecture we learnt the difference and uses of the three built-in functions as they are being used as a predefined variables and these are also called super global variables in PHP programming. These three variables works on HTTP protocol to create a communication between the client and server.

And also we used the form tag to design a contact form webpage. And inside the form tag in the action we wrote the name of the webpage file to whom we want to transfer the data after the authentication of the HTTP.

Access Modifiers in PHP

PHP & MySQL Course: Access Modifiers in PHP

In previous lecture 10, we learnt about the important keywords and also about the abstract class. And we implement them in examples which become so easy for us to understand their concepts according to the OOP in PHP.

Lecture 11:Access Modifiers in PHP

In this lecture 11, we will learn about the modifiers in PHP. As we learnt modifiers in Java and C++ programming. The modifiers are used to access the data in different ways. Topics are:

  1. Private access modifier in PHP
  2. Public access modifier in PHP
  3. Protected access modifier in PHP

Private Access Modifier in PHP:

The private access modifier role in PHP is same as it is being used in Java and C++ OOP. We use the Private access Modifier in PHP to hide the data from the user. Its mean that the data will be used by the application and the data will be accessed inside the class or methods where they will be declare as private. Let’s check the Listing 1 about the Private Access Modifier in PHP

Public Access Modifier in PHP:

The public access modifier is used, the data is available for all as public. In this case, the data can be use anywhere. There is a example about the public access modifier in Listing 1.

Listing 1: Private & Public Access Modifier in PHP


<? class Labtop {

private $Model = "Dell Studio 1569";

private      $HardDrive = 500;

private              $Ram = 8;

public function Specification(){

return $this->Model;

}

}

class User extends Labtop{

public $UserName = "Ashley";

public $UserExperince = "Engineering";

public function UserInformation(){

echo $this->UserName." has a ";

echo $this->Specification();

}

}

$obj = new User;

echo $obj->UserInformation();

?>

Protect Access Modifier in PHP:

The Protected access Modifier is used the middle use of Public and Private. For example if we want to give the limited permission to the user to use the data. So we commonly use the Protected access modifier in PHP during the development of the web pages. Let’s check the Listing 2 for details about the protected access modifier. When we use the Protected in class so its mean that the data will be able to access inside the class and its child class too.

Listing 2: Protected Access Modifier in PHP


<?

class Labtop {

protected $Name = "Dell Studio";

public function NameDisplay1(){

echo "Accessing inside the Parent Class: ".$this->Name;

}

}

class Display extends Labtop{

public function NameDisplay2(){

echo "<br>Accessing inside the child Class: ".$this->Name;

}

}

$obj = new Labtop;

echo $obj->NameDisplay1();

$obj = new Display;

echo $obj->NameDisplay2();

?> 

Description:

In the listing 1 and 2 as you can see the usages of the access modifiers in PHP. In the listing 1 there are two access modifiers has been used. Public and Private, and these are fully understandable due to their name. The data which are public they can access from anywhere and the private data can be access just inside the class or where they declared once.

And in the listing 2, the protected access modifier in php is used. The data in which the protected access modifier is used they can just access inside their class inside the child class.

Summary:

These access modifiers in php are used to secure the data. And we also use them to make the access of the data from the use as limited and unlimited. These access modifiers are being used according to the data.

Abstract – Interface keywords in PHP

PHP & MySQL Course: Abstract – Interface keywords in PHP

In the previous lecture 9, we learnt about the inheritance and polymorphism.

And we also used these concepts in the examples to understand them.

Lecture 10:

In this lecture 10, we will discuss the more advanced topics of the PHP

programming. And these will help us in dynamic website development. Topics are:

  1. Abstract class in PHP
  2. Interface keyword in PHP
  3. Implements keyword in PHP

Abstract Class in PHP:

As you know now we are in advanced PHP programming. And we are learning

the concepts of the OOP in PHP programming.

We use abstract class to hide the data in the classes. And when we use the abstract

class so it’s mean that we can’t use or declare any object for the abstract classes.

We use the abstract class to inherit it with the child class. As you saw how we can

inherit the classes in PHP programming in our previous lectures. In this lecture you

will learn how you can inherit the abstract classes in PHP. We also use the same

keyword here as extends. Check the listing 1 for details about the abstract classes in PHP.

Listing 1: Abstract Class in PHP

<br /><br />&lt;?<br /><br />abstract class Student{<br /><br />function StudentInformation()<br /><br />{<br /><br />$record = $this-&gt;Records();<br /><br />return $record;<br /><br />}<br /><br />abstract function Records();<br /><br />}<br /><br />class Teacher extends Student{<br /><br />function Records(){<br /><br />return 200;<br /><br />}<br /><br />}<br /><br />$print = new Teacher;<br /><br />echo "Total Students are: ".$print-&gt;StudentInformation();<br /><br />echo "&lt;br&gt;";<br /><br />?&gt;<br /><br />

As you can see in the listing 1, we declared an abstract class named Student.

And then we inherit it with Teacher class. And to use the hidden data

of the abstract class, we declared an object of the Teacher class and then

called the function or method of the Student abstract class by the object of the Teacher class.

Use of Interface in PHP:

The interface is a keyword in PHP. And we use it with classes in PHP programming for

developing the dynamic websites. And the interfaces are purely abstracted classes in PHP.

And we use the implement keyword to use the interface with methods.

Listing 2: Interface in PHP

<br /><br />&lt;?<br /><br />interface Information{<br /><br />public function DisplayData();<br /><br />}<br /><br />class Records implements Information{<br /><br />public function DisplayData()<br /><br />{<br /><br />echo "&lt;b&gt;Website Name:&lt;/b&gt; http//:www.trustingeeks.com";<br /><br />}<br /><br />}<br /><br />$object = new Records;<br /><br />echo $object-&gt;DisplayData();<br /><br />?&gt;<br /><br />

As you can see in the Listing 1 & 2, these both programs are doing same work.

And we used some keywords which are common in both programs i.e extends.

But there is a pretty difference between the abstract class and interface.

Difference Between Abstract Class and Interface:

  1. As you know we use abstract keyword to define the abstract class and same like this we also use it for methods. The important thing is that it is not important every method should be abstract in an abstract class but in an interface it is necessary.
  2. There is a concept of multiple and multilevel inheritance in PHP. The multilevel and single inheritance is being used in abstract classes because it is possible.
  3. There are four access modifiers which we can use them for methods. As we can just use public access modifier in the case of interface and in abstract classes we can use other all with public too.
  4. In abstract we can declare and also define the methods. But in interface we can’t do it, because we can just declare it then we will defined it somewhere else.

Summary:

In this lecture we learnt most important and useful topics about the OOP in PHP

web pages programming. These things are useful when we want to secure our data on the web pages.

Polymorphism-Inheritance in PHP

PHP & MySQL Course:Polymorphism-Inheritance in PHP

In the previous lecture 8, we learnt about OOP in PHP programming. We deal with classes and objects

in OOP as we learnt in the lecture 8.

Lecture 9:Polymorphism-Inheritance in PHP

In this lecture we will also follow the class and objects. But in this lecture we will discuss the

concept of following topics:

  1. Inheritance in PHP with example
  2. Polymorphism in PHP
  3. $this variable in PHP

Inheritance in PHP with exaples:

When we use the concept of the inheritance, at that time we declare a base class and after that

we declare a drive class same like the base class. We can create or add methods or functionalities in the

drive class in case of base class. The inherit concept extends the communication of two classes. For

example there are two classes A and B. in this case the B is drive from the A class. Now lets see the

listing 1 for details about the Inheritance and we will use a keyword as extends.

Listing 1: Inheriting the classes


<?

class Labtop {

private $Model = "Dell Studio 1569";

private         $HardDrive = 500;

private         $Ram = 8;

public function Specification(){

return $this->Model;

}

}

class User extends Labtop{

public $UserName = "Ashley";

public $UserExperince = "Engineering";

public function UserInformation(){

echo $this->UserName." has a ";

echo $this->Specification();

}

}

$obj = new User;

echo $obj->UserInformation();

?>

In this example there are two classes first class is Labtop which is base class and the other class is User

which is derived class. And we inherit them by using the keyword extends, so by this way we can inherit

the classes in PHP programming.

Polymorphism in PHP:

First of all we should know, what is polymorphism? So dear friend’s polymorphism means that many

forms. In simple words there are many methods but they are sharing same interface. And in PHP the

polymorphism is not possible due to some problems. But in PHP5 there is a way to use the concept

of the Polymorphism in PHP programming for dynamic websites. Lets check the listing 2 for details

about the polymorphism in PHP programming.

Listing 2: Polymorphism in PHP


<?

class Labtop {

public function Specification() {

echo "Labtop Class method called...";

}

}

&nbsp;

class User extends Labtop  {

public function Specification() {

echo "User Class method called";

}

}

&nbsp;

function CallingMethods(Labtop  $call) {

$call->Specification();

}

&nbsp;

$Object = new User();

CallingMethods($Object);

?>

We used the same class of listing 1, but here in listing 2 we apply the polymorphism concept as you can see it.

And now check the listing 3 for details about the overridden of methods in PHP using the polymorphism.

Listing 3: Overridden in PHP


<?

class Labtop {

public function Specification() {

echo "Labtop Class method called...";

}

}

&nbsp;

class User extends Labtop  {

public function Specification() {

echo "User Class method called";

}

}

&nbsp;

function CallingMethods(Labtop  $call) {

$call->Specification();

}

&nbsp;

//$Object = new User();

$Object = new Labtop ();

//CallingMethods($Object);

$Object->Specification();

?>

In this case the methods are overridden and just one method is being called due the object of the class.

In the listing 2, we declared a method for calling the methods that is also calling the same thing which

we done in the listing 3. So it is cleared that listing 2 and listing 3 are overridden methods and the

polymorphism is not possible in PHP.

Summary:

In this lecture learnt about the inheritance and polymorphism. We will also use these two terms in our

coming lectures more, here we just learnt some introduction about them. And I also used the $this variables

which you can check it in the listing 1.

These two topics are the heart or you can say that are the soul of the OOP. Because OOP depends on

class and objects. And here in class and objects are just to solve the problems. We will create little

projects using the PHP programming, which will clear our concepts which we learnt in this PHP

and MySQL course.

PHP OOP

PHP & MySQL Course:  PHP OOP

In the previous lecture 7, we learnt about the how we can use the HTML tags in PHP, and we use the

dot operator to concatenate the strings in PHP and we also discussed the display functions as they

are echo() and print().

Lecture 8 PHP OOP:

In this lecture you will learn about the most important topics, from which we use the OOP concepts in PHP.

The topics from php oop  are following:

  1. Creating Classes in PHP
  2. Declaring Objects in PHP
  3. Using constructions in PHP
  4. Using keywords with in class in PHP

Creating Classes in PHP:

First of all we should know about the class. The class concept comes from the link, I mean we use class when

we are targeting one thing in details. As if we create a class and name it student, so there are some attributes of the

student. Which are student name, student roll number, blab blab blab. Like this there are many things we can create

classes for them. By using the class our data is secure and we don’t know what is happening in the class during the

execution of the methods which are inside of the class. So we just know that for what purpose we can use the class.

For example people commonly using the internet they can access any website using the website name or address.

When they write the website address in the browsers address bar and press enter. Then what happen just nothing

the website comes in front of them. But they don’t know how it happens in the back end. In PHP programming we

use class keyword to declare the classes and then we use the class name to name it.

Syntax:

Class className{

Constractors…..

Methods……

Variables…..

Statements……

}

Listing 1: Class in PHP Programming

</pre>
<?

class Student{

public function StudentInformation()

{

$name = "Ashley";

$rollnumber = 12344;

echo "Name of the student is ".$name;

echo "Student Roll Number is ".$rollnumber;

}

}

$Student1 = new Student;

$Student1->StudentInformation();

?>
<pre>

In the listing 1, I declared a class using the keyword class and used the Student as name. Inside the class there

is a method name StudentInformation which contains data about the students. And we called the

method using the $Student1 as an object. In the PHP declaring an object is little bit different as we usually

declare any object in Java and C++ object oriented programming languages oop. If you want to get information

about the object and class so use the var_dump() function. For details check the listing 2.

Listing 2: var_dump()

</pre>
<?

class Student{

public function StudentInformation()

{

$name = "Ashley";

$rollnumber = 12344;

echo "Name of the student is ".$name;

echo "Student Roll Number is ".$rollnumber;

}

}

$Student1 = new Student;

$Student1->StudentInformation();

var_dump($Student1);

?>
<pre>

By the help of the built-in method var_dump(), we can check the contents of the class. This method shows the

details about the class when we execute the program on the browser.

As we can also use the public keyword in the class with any variable or method. By which the variable and

method will be easily access outside the class. We do this commonly with the variables. Lets we try

to use the public keyword with variables in PHP.

Listing 3: Using Public keyword in class

</pre>
<?

class Student{

public $display = "http://www.trustingeeks.com";

public function StudentInformation()

{

$name = "Ashley";

$rollnumber = 12344;

echo "Name of the student is ".$name;

echo "Student Roll Number is ".$rollnumber;

}

}

$print = new Student;

echo $print->display;

?>
<pre>

We used the public keyword in listing 3, there is a variable and a method which both are public. Its mean that

these both can be used them outside the class easily. We can also access the methods of the class without using the

public keyword.

Constructors & Desctructors in php oop:

In PHP we declare or define constructors in a different way. As usually we declare or define the methods in

Java and C++ by the same name as the class have. But in PHP programming we use a keyword as

__construct(). And we don’t need to call it by self. When we declare any object in PHP programming for the class,

at that time the __construct() method called and executes. Lets try it in listing 4 for details.

Listing 4: Constructors in PHP Programming

</pre>
<?

class Student{

public function __construct(){

echo "The class is called without any trouble...<br>";

}

function StudentInformation()

{

$name = "Ashley";

$rollnumber = 12344;

echo "Name of the student is ".$name;

echo "Student Roll Number is ".$rollnumber;

}

}

$print = new Student;

echo $print->StudentInformation();

?>
<pre>

As you can see in the listing 4, declaring on object we called two methods. As the constructor is also a method. Now

we will destroy the constructor using the __destruct() keyword. When we will declare an object so it will also destroy

the constructor. Let’s check the listing 5 for details in which we will also check the class name by using the keyword

__CLASS__, this keyword will tell us about the name of the class.

Listing 5: Destructors in PHP

</pre>
<?

class Student{

public function __construct(){

echo 'The class "',__CLASS__,'" constructor is created...<br>';

}

public function __destruct(){

echo 'The class "',__CLASS__,'" constructor is destroyed...<br>';

}

function StudentInformation()

{

$name = "Ashley";

$rollnumber = 12344;

echo "Name of the student is ".$name;

echo "Student Roll Number is ".$rollnumber;

}

}

$print = new Student;

echo $print->StudentInformation();
<pre>echo "<br>";
?>

Summary:

In this lecture we started the object oriented programming in PHP. The concept of the OOP should be clear

in details.Because if the programmers don’t know about the OOP in details or their concepts are not clear so it

will be difficult for them to develop any website as a dynamic.

In this lecture we learnt about the class and its objects. Using the class we use the keywords as there are: public,

__construct() and __destruct() these keywords are very important to be known for a programmer. Because

the OOP concept of the PHP depends on them. And we also used the __CLASS__ keyword to take information

about the class as its name.

Embed  PHP in HTML

PHP & MySQL Course:Embed  PHP in HTML

In the previous lecture 6, we learnt about the functions as there are built-in and user-defined functions in very

programming language. We also discussed the sorting arrays PHP, and used built-in functions of PHP

programming to sort the arrays in an easy.

Lecture 7:Embed  PHP in HTML

In this lecture we will study, how we can use the HTML tags in PHP programming. And we need sometimes to

use the HTML tags in PHP programming. Following are the topics of lecture 6.

  1. HTML tags in PHP
  2. Concatenation operator in PHP
  3. Use of variables in PHP with display functions

HTML tags in PHP:

There are many tags in HTML which we can use them in PHP programming. By using of them we feel easy

to designvdynamic web pages. We use commonly tags of HTML in PHP programming as there are heading

tags, break tag etc. Check the listing 1 for details about the HTML tags in PHP. There is an example in the

listing 1, from which you will understand how you can use the HTML tags in PHP.

Listing 1: Use of HTML tags in PHP

//using the html table tag in PHP to create a table

<?php

echo " <h1>HTML tags in PHP...</h1>" ;

 

?>

 

Concatenation Operator in PHP:

We use dot operator as a concatenation operator in PHP. Due to dot operator we can join any two strings.

And we use it during the display of variables. At that time we use dot operator to display the data of the

variable in echo or print function. We can also display the data of the variables in PHP if we use the

variable of the PHP inside the double quotes. For details about the concatenation check the listing 2.

There is a simple example from which you will learn how you can use dot operator. And you

will be able to understand where you should use the dot operator.

Listing 2: Concatenation Operator in PHP

<?php
//use of dot operator in PHP
$name1 = "Trust";
$name2 = "In";
$name3 = "Geeks";
$FullName = $name1.$name2.$name3;
echo $FullName;
//an other way to use dot operator in PHP
echo "The Full name of the website is: ".$name1.$name2.$name3;
 ?>php

Use of variable in PHP:

There is a way how to use the variables in PHP. As we commonly declare the variables and assign them values.

After that we use them in the program for more process. But we can use them to declare, assign and use them

for any process at the same time. Check the listing 3 for details about the variable uses in PHP programming.

You will see something new to learn about the PHP variables.

Listing 3: Variable use in PHP

<?php
//using variables in PHP with dot operator
$name = "TrustInGeeks";
echo "My Website Name is: ".$name;
print "My Website Name is: ".$name;
//using variables in PHP without dot operator
//we will use variables in PHP inside the double quote
echo "My Website Name is: $name";
print "My Website Name is: $name";
?>php

Summary:

In this lecture we learnt something important about the use of variables and dot operator in PHP.
Form using of the dot operator in PHP I shown you how you can concatenate the two different
strings in PHP. We also discussed the HTML tags, we can use all HTML tags in the PHP programming.
And there is no any kind of violation in PHP to avoid the HTML tags. We can use them without
any hesitation.

Built in User Defined Functions in PHP

PHP & MySQL Course:Built-in User Defined Functions in PHP

In the previous lecture 5, we learnt something important about the comments, loops and arrays which helps us too

much during the website development. When we are using PHP & MySQL.

Lecture 6:Built-in  & User Defined Functions in PHP

In this lecture we will learn advanced PHP programming. In this lecture you will able to understand the uses of

functions in PHP and also some built-in functions. As we know that there are two types of functions Built-in functions

and user-defined functions.

  1. Built-in Functions in PHP
  2. User defined Functions in PHP
  3. How to sort arrays in PHP

Built-in Functions in PHP:

The built-in functions are those functions which are derived by the PHP designers. And we can just use them whenever

we feel need of them. Because we don’t know which type of programming had been done inside that built-in function,

we just know how to use it and for what we can use it? There are list of built-in functions i.e.

  1. imagecreatetruecolor()
  2. mysql_connect()
  3. phpinfo()
  4. get_loaded_extensions()
  5. str_replace()
  6. usort()
  7. function_exits()
  8. dl()
  9. imagecolorallocate()
  10. imagestring()
  11. imagepng()
  12. imagedestroy()

There are just 11 built-in functions in this lecture, but you can find many built-in functions in PHP. And you can

use them in your dynamic web pages. These built-in functions made easy to design a dynamic website with the

help of PHP programming. And we should keep it in our minds the PHP is a server side language. Check listing 1 for

details about the built-in functions there is an example about the image related function.

Listing 1:what are Builtin Functions in PHP


<?

//creating an image

$ImageToCreate = imagecreate( 270, 80 );

//using color for image background to be fill

$ImageBackground = imagecolorallocate( $ImageToCreate, 128, 255, 0);

//using color for text which to be write on image

$ImageTextColor = imagecolorallocate( $ImageToCreate,0, 0, 255 );

//using underline for text

$ImageUnderLineText = imagecolorallocate( $ImageToCreate, 255, 255, 0 );

imagestring( $ImageToCreate, 16, 30, 25, "Welcome To TrustInGeeks",

$ImageTextColor );

//underline size

imagesetthickness ( $ImageToCreate, 10 );

imageline( $ImageToCreate, 29, 45, 238, 45, $ImageUnderLineText );

//creating the image on the browser

header( "Content-type: image/png" );

imagepng( $ImageToCreate );

imagecolordeallocate( $ImageUnderLineText );

imagecolordeallocate( $ImageTextColor );

imagecolordeallocate( $ImageBackground );

imagedestroy( $ImageToCreate );

?>

builtinfunctions

Figure 1  Output of the Listing 1

User-defined Functions in PHP:

We can also create functions using our own algorithm. And we use functions to avoid writing programming codes

multiple times. For example if we want to display any name ten times as we can use it by loop. But if you don’t know how

to use loop, so write the echo function and print the name ten times. And do this programming in a function. Now

when you need to write those ten names or ten time a single name, so just call the function and then see the program.

From using the built-in functions we can save our time, and also we can write programs very fast. There are four

types of user-defined functions in programming languages commonly. As they are

  1. void FunctionName(void)
  2. void FunctionName(Argument)
  3. Argument FunctionName(void)
  4. Argument FunctionName(Argument)

In PHP programming we use them same as we used user-defined functions in C++ and Java programming Languages.

For details about these check the listing 2, to get idea how to use the user-defined functions in PHP programming.

But in PHP programming we use function keyword to declare or create any function/method.

Syntax:

Function FunctionName(){

Statements…..

}

Listing 2: User-Defined Functions in PHP


<?

$FirstNumber = 10;

$SecondNumber = 10;

$Addition =  AddTwoNumbers($FirstNumber,$SecondNumber);

$Subtraction = SubtractTwoNumbers();

$PrintNumbers = PrintOnScreen($FirstNumber,$SecondNumber);

//Calling the DisplayFunction

DisplayFunction();

echo "<br>Addition of Two numbers are: ".$Addition;

echo "<br>Subtraction of Two numbers are: ".$Subtraction;

function AddTwoNumbers($FirstNumber, $SecondNumber)

{

//the type of this function is Argument FunctionName(Argument)

return $FirstNumber + $SecondNumber;

}

function SubtractTwoNumbers()

{

$New1 = 30;

$New2 = 40;

//the type of this function is Argument FunctionName(void)

return $New1 - $New2;

}

function DisplayFunction()

{

//the type of this function is void FunctionName(void)

echo "<h4>We Are Learning The User-Defined Functions</h4>";

}

function PrintOnScreen($FirstNumber,$SecondNumber)

{

//the type of this function is void FunctionName(Argument)

echo "The First Number is ".$FirstNumber;

echo "<br>The Second Number is ".$SecondNumber;

}

?>

How to Sort Arrays in PHP:

Dealing with Arrays in any programming language is a difficult job. But there are some built-in functions in PHP, by

which we can deal with the arrays fully easy. As we learnt in our previous lecture number 5 about the arrays. There are

about to three types arrays in PHP programming. And how to sort them there are some built-in function. Which made

easy for us to sort the arrays easily. And we use them for different purpose, because when we deal with arrays in PHP

programming. So there are associative arrays too. So it’s not easy to sort them without any function, we can use any

algorithm to use it and make it easy. But we should think about it. It will take long time. To create a new algorithm for

these kind of issues. So let’s we use the built-in functions for sorting the arrays.

  1. ksort()
  2. sort()
  3. arsort()
  4. krsort()
  5. assort()
  6. rsort()

We use these 6 built-in functions to sort the arrays, with different methods. As we use sort() function for ascending

order of arrays. As we use ksort() function for using the key of associative arrays to sort them in ascending form. As we

use arsort() function for using the key of associative arrays to sort them in descending form. As we use krsort() function

for using the key of associative arrays to sort them in descending form. As we use assort() function for using the key

of associative arrays to sort them in ascending form. And as we use arsort() function for using the arrays to sort them

in descending form. See the listing 3 for details about the sorting of arrays in different methods.

Listing 3: Sorting Arrays in PHP


<?

$number = array(0,9,8,7,6,5,4,3,2,1);

$name = array("Tino","Bucky","Ashley","Zeaf");

$record = array("Tino" => "20", "Bucky" => "10", "Ashley" => "23", "Zeaf" => "90");

//using sort function to sort the data in array

sort($number);

for($x=0;$x<count($number); $x++)

{

echo $number[$x]."<br>";

}

//using rsort function to sort the data in array

rsort($name);

for($x=0;$x<count($name); $x++)

{

echo $name[$x]."<br>";

}

//using ksort function to sort the data in array

ksort($record);

foreach($record as $x => $value)

{

echo $value."<br>";

}

//using the arsort funciton to sort the data in array

arsort($record);

foreach($record as $x => $value)

{

echo $value."<br>";

}

//using the krsort function to sort the data in array

krsort($record);

foreach($record as $x => $value)

{

echo $value."<br>";

}

?>

Summary:

In this lecture 6, we learnt about the two types of functions as built-in functions and user-defined functions. And there

are more four types of functions in user-defined functions.

And we learnt about the sorting of arrays in PHP programing using the built-in functions of PHP. And it is easy

to sort the PHP arrays using the built-in functions.

PHP FOR LOOP – PHP ARRAY

PHP & MySQL Course:PHP FOR LOOP –  PHP ARRAY

In the previous lecture# 4 Part A & B, we learnt about the operators and theirs uses and also conditional

statements. We learnt these all topics in detailed and also defined them in programs.

Lecture 5:PHP FOR LOOP –  PHP ARRAY

In lecture 5, we are going to learn few more important topics. And these topics are commonly

use in every object oriented programming languages. These topics made easy to do programming

in PHP as well as others too.

  1. Loops in PHP
  2. Array in PHP
  3. Comments in PHP

Loops in PHP:

These are also the same types of loops and their use is also same, like other programming languages.

As for loop, while loop, do while loop, and foreach loop. These loops are being used for different

purposes and they solved many problems. Sometimes we need to write a single name or word and

any sentence many times may be 10 to 50 times. So we use loops to write it,

and loop writes it in seconds.

Syntax:

  1. for(initial condition; final condition; increment/decrement) {statement}
  2. while(condition){statement}
  3. do{statement}(condition);
  4. foreach(condition){statement}

PHP For loop:

For loop, we know how much times the iteration will runs and we also know when it

will be break or stop the iteration of the loop. See the listing 1 for details.

Listing 1: Using PHP For Loop


<?

//using for loop in PHP

$var1 = 10;

$name = "TrustInGeeks";

for($var2 = 1; $var2<$var1; $var2++)

{

echo $name." ";

}

?>

While loop:

While loop, in this loop we use a condition. If the condition is true then it

will stop the running iterations. See the listing 2 for details.

Listing 2: Using While Loop in PHP


<?

//using while loop in PHP

$var1 = 1;

$name = "TrustInGeeks";

while ($var1 <10)

{

echo $name." ";

$var1++;

}

?>

Do while loop:

Do while loop, in this loop we also use a condition. But the difference is that it

will run once at any cost. It means that we can use this loop where we need it.

See the listing 3 for details.

Listing 3: Using Do While Loop in PHP


<?

//using do while loop in PHP

$var1 = 1;

$name = "TrustInGeeks";

do

{

$var1++;

echo $name." ";

}while($var1 < 10);

?>

Foreach loop:

Foreach loop, we use this loop commonly in arrays. And we know working

with arrays is complicated, so we need to be very care full. See the listing 4 for details.

Listing 4: Using Foreach Loop in PHP


<?

//using foreach loop in PHP

$name = array("a" => "C++","b" => "Java","c" => "PHP & MySQL","d"
=> "JavaScript",);

foreach($name as $ArrayData)

{

echo $ArrayData;  //now we will print the data of array using $ArrayData

}

?>

Using   PHP Array:

As we know that the array is used to define list of similar data in a single variable.

And there three types of Arrays are being used in PHP programming for web pages design.

  1. Numeric Arrays in PHP
  2. Associative Arrays in PHP
  3. Multidimensional Arrays in PHP

Numeric Arrays in PHP:

The numeric arrays are used to store numbers, strings, letters, and any type of other object.

But we use a keyword array() to declare the arrays. Its name is numeric arrays due to its

counting of items in the array. As it starts from the 0 to the last item. It mean that its

first item will be at 0[zero]. Check the listing 5 for details

Listing 5: Using Numeric Arrays in PHP


<?

//using numeric arrays in PHP

$number = array(1,2,3,4,5,6,7,8,9,10);

for($i = 0; $i <= 9; $i++)

{

echo "<br>".$number[$i];

}

//different way to define numeric arrays in PHP using item number

$name[0] = "C++";

$name[1] = "Java";

$name[2] = "PHP";

$name[3] = "MySQL";

$name[4] = "JavaScript";

//display them using their item numbers

echo "<br> $name[0]";

echo "<br> $name[1]";

echo "<br> $name[2]";

echo "<br> $name[3]";

echo "<br> $name[4]";

//displaying them with loops

foreach ($name as $i)

{

echo "$i<br>";

}

Associative Arrays in PHP:

The associative arrays are used by their index. Because these are called associative arrays

due to the connection of data with index. And it is not necessary to use any number for the index.

We also use any string or letter for the index. And then we assign it an item or data. We never say it,

it is being start from the 0. It just starts from where it is displaying the data. We will also use the

array() method to declare the associative arrays in PHP . Check the listing 6 for details about

associative arrays.

Listing 6: Using Associative Arrays in PHP:


<?

//using associative arrays in PHP

$grade = array ("C++" => "A", "Java" => "A+", "PHP" => "A");

foreach($grade as $index => $value)

{

echo "$value is on $index<br>";

}

/*there is an other simple way to declare the

associative arrasy in PHP as we declared the numeric arrays in PHP*/

$grade["C++"] = "A";

$grade["Java"] = "A+";

$grade["PHP"] = "A";

$grade["C#"] = "A";

//we can also print them without any using loop

echo $grade["C++"]." ".$grade["Java"]." ".$grade["PHP"]." ".$grade["C#"];

?>

Multidimensional Arrays in PHP:

From its name you may understand, we will work in rows and columns now. We can set that

data in rows and columns. And we call one row as a record. So now we will use the multidimensional

arrays of PHP in our programs. It is difficult job to work with multidimensional arrays and also it is

not being used commonly in websites development. We use commonly the numeric and associative

arrays during the development of websites. Read the listing 7 for details about the multidimensional arrays.

Listing 7: Use of Multidimensional Arrays in PHP:


<?

//using multidimensional arrays in PHP

$records = array(

"John" => array("age" => 40,"city" => "New York", "work" => "unemployee"),

"Toney" => array("age" => 20,"city" => "Bejing", "work" => "Computer Operator"),

"Tina" => array("age" => 30,"city" => "Washington", "work" => "Model")

);

//using with foreach loop

foreach($records as $i => $value)

{

echo "<h5>Data for $i "."</h5>";

foreach($value as $j)

{

echo $j."<br>";

}

}

?>

Comments in PHP:

Using of comments and its purpose is same in PHP. We use comments to remind us when

we want to check the source code.And it is very good habit to use comments. There are two

types of comments single line comments and multi-line comments. For single line comments

we use double slash as “//” and for multi-line comments we use “/**/”. And the

comments never execute. These are dummy lines in any source code of any   programming

languages. As we used comments in all listing which you can understand the source code easily.

Summary:

In this lecture 5, we discussed the three topics. And these three topics are very important to understand them

for future. Because these topics are being used commonly in website development. We discussed the loops

and their types. Then we used them in programs to execute and checked their results. After that we used the

most important topic array and then we also use them in programs using PHP. And then we discuss the

simplest topic comments as you are familiar with it.

Conditional Statements And Logical Operators in PHP

PHP & MySQL Course: Conditional Statements And Logical Operators in PHP

In the previous article we learnt about the operators of PHP and used those in different ways with variables the lecture 4 part B is now continue.

Lecture 4 (Part-B):Conditional Statements And Logical Operators in PHP

In this article I continue the remaining topics of lecture 4. It was getting time and being so long that’s why I break it in two parts. By which you can understand them easily. Because now we in going to ahead in PHP & MySQL gradually.

  1. Use of Operators in PHP
  2. Use of Conditional statements in PHP

Logical Operators in PHP:

The usage of the Logical Operators in PHP are same as in C++ and Java programming languages. There is no any different methodology to use them in PHP. And there is no any extra Logical operator in PHP. These are same as in C++ and Java or other programming languages. For details about the logical operators see the table 1 for their use. But we use them also in English words also.

Table 1  Logical Operators in PHP

Name of Operators

Symbol

Description

Use in PHP

And Operator

and

Returns true when both are true

$a and $b

Or Operator

or

Returns true when any one is true

$a or $b

Xor Operator

xor

Returns true when any one is true, not both

$a xor $b

And Operator

&&

Returns true when both are true

$a && $b

Or Operator

||

Returns true when any one is true

$a || $b

Not Operator

!

Returns true when variable values is false

!$b

Comparison Operators in PHP:

We use multiple operators in PHP for comparing the data. Because it is very important to compare the data which type of data is coming from the user. There are multi-types of websites. And they need to compare data and then proceed the process. Without comparing the data, it is very difficult to give good facility to the user. And also it will be a difficult job to secure the website from the hackers. See the table 2 for more details.

Table 2  Comparison Operators in PHP & MySQL

Name of Operators

Symbol

Description

Use in PHP

Equal Operator

==

It compares two variables conditions

$a == $b

Identical Operator

===

It compares two variables conditions with stress

$a === $b

Not Equal Operator

!=

It compares the valid values of two variables

$a != $b

Not Equal Operator

<>

It compares the valid values of two variables

$a <> $b

Not Identical Operator

!==

It compares the valid values of two variables with stress

$a !== $b

Greater Than Operator

>

It checks the largest value

$a > $b

Less Than Operator

<

It check the smallest value

$a < $b

Greater Than & Equal to Operator

>=

It compares and checks the largest value

$a >= $b

Less Than & Equal to Operator

<=

It compares and checks the smallest value

$a <= $b

Array Operators in PHP:

There are some operators in PHP which we use to compare the arrays in PHP. Sometimes we need to compare the data of the operators and also sometimes we need to compare their keys. It is a very complicated task, to compare the one array data or key of array with other array or key of array. Check the table 3 for details about the array operators in PHP programming which are being used in web pages.

Table 3 Array Operators in PHP

Name of Operators

Symbol

Description

Use in PHP

Union Operator

+

It takes the Union of two arrays if there is no any duplicate key

$a + $b

Equality Operator

==

It takes the Equality to check the keys are same or not

$a == $b

Identity Operator

===

It checks the Identity of two arrays with their keys.

$a === $b

Inequality Operator

!=

It checks the equality and return true when not equal with each other

$a != $b

Inequality Operator

<>

It checks the equality and return true when not equal with each other

$a <> $b

Non-identity Operator

!==

Its check the Identity of two arrays with their keys

$a !== $b

Conditional Statements in PHP:

In conditional statements we will discuss the following statements.

  1. If(){…} Statement in PHP
  2. If(){…} else{…} Statement in PHP
  3. If(){…} elseif(){…} else{…}Statement in PHP
  4. Switch() {…} Statement in PHP

Description of Conditional Statements in PHP:

We use the first statement “If() {…}” when we are just comparing two variables or checking the Boolean (True or False) conditions of any variable. And we use the 2nd conditional statement to do same as we use If(){…} but it will go to else {…} condition when it is false, or which we want from the If(){…} condition but it not happen so it will do else{…} condition command.

And we use if(){…}elseif(){…}, we want to do multiple comparisons in a single program of PHP. It will work continuously by checking the if(){…} statement with else{…} condition.

And we use switch(){…} conditional statement in PHP for same purpose. But it just once check the operation in switch() statement and then check different conditions in the block. Using of switch is very simple than If(){…}, if(){…}else{…} and if(){…} elseif(){…} conditional statement. Check listing 1,2,3 & 4 for details about Conditional Statements.

Syntax:

If(condition) {statement}

If(condition) {statement} else {statement}

If(condition) {statement} elseif(condition){statement} else {statement}

Switch(operation){case 1, case 2 … deafualt}

Listing 1: If(){…} Statement


<?

$a = 3;

$b = 4;

if($a < $b)

{

echo "$a is less than $b";

}

?>

Listing 2: If(){…} else{…} Statement


<?

$a = 4;

$b = 4;

if($a < $b)

{

echo "$a is less than $b";

}

else

{

echo "$a is equal to $b";

}

?>

Listing 3: If(){…} elseif{…} else Statement


<?

//using if elseif condition statement for check the smallest number

$a = 1;

$b = 2;

$c = 3;

if($a > $b && $a > $c)

{

echo "$a is the largest number";

}

elseif ($b > $a && $b > $c)

{

echo "$b is the largest value";

}

elseif($c > $b && $c > $a)

{

echo "$c is the largest value";

}

else

{

echo "$a ,$b & $c are equal to each other";

}

?>

Listing 4: Switch(){…} Statement


<?

$a = 4;

$b = 3;

switch($a % $b == 1)

{

case True:

echo "<br>The Remainder of $a and $b is 1";

break;

case False:

echo "<br>The Remainder of $a and $b is not 1";

break;

default:

echo "<br>Something gone wrong in Calculation... Sorry!!!";

}

?>

Listing 5: Array Operators with Conditional Statement


<?

$a = array("a" => "C++", "b" => "Java");

$b = array("c" => "PHP", "d" => "MySQL");

$c = $a + $b; // union of $x and $y

if($a == $b)

{

echo "Equal, Keys or Pair values are same...";

}

else

{

echo "Not Equal";

}

echo "<br>";

if($a === $b)

{

echo "Equal, The Identity of keys are True...";

}

else

{

echo "Not Equal";

}

echo "<br>";

if($a <> $b)

{

echo "Its Ture, both are not equal...";

}

else

{

echo "Equal";

}

echo "<br>";

if($a !== $b)

{

echo "Equal, Their Keys Identity is True.";

}

else

{

echo "Not Equal";

}

?>

Summary:

In lecture 4, we learnt about two most important topics first operators. In operators we learnt about different types of operators as arithmetic, assignment, increment and decrement, comparison, logical, and also those operators which used in arrays called array operators.

And we also discussed the conditional statements. We use them in different programs using PHP programming syntax.