Archive

Category Archives for "Php & MySql"

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

Operators in PHP

PHP & MySQL Course:Operators in PHP

In the previous lecture we learnt about the basics of PHP and used variables in different types.

Lecture 4 (Part-A):Operators in PHP

In this lecture we will learn the following topics. And you should practice todays lecture by your

heart because these are one of the most important topics.

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

Operators in PHP:

We use same operators in PHP & MySQL, which we used in C++ and Java Programming Languages.

But in PHP we will use them in a little bit change method. Because there are some operators are look

different from the C++ and Java Programming Language operators. Which you had been used. Let’s

we see the list of operators and their use.

Use of Arithmetic Operators in PHP & MySQL:

We use Arithmetic Operators for simple calculation in PHP programming. And these are very common

to use in other different type of languages. By the help of Arithmetic Operators in PHP programming it

becomes so simple now to create a web page for any economic institute. Check listing 1 for details how

we can use the Arithmetic Operators in PHP & MySQL.

Table 1     Arithmetic Operators in PHP & MySQL

Name of Operator

Symbol

Description

Use In PHP

Addition

+

Sum two variables

$a + $b

Subtraction

Subtract two variables

$a – $b

Multiplication

*

Multiply two variables

$a * $b

Division

/

Divide two variables

$a / $b

Modulus

%

Take remainder of any two variables

$a % $b

Use of Assignment Operators in PHP & MySQL:

We use Assignment Operators for comparison of values in PHP programming. By using them we can

compare anything in PHP webpages. These all assignment operators are same like which you had been

used in C++ and Java Programming languages. So don’t get worried about them. These are fully easy to

use them. And see the Listing 2, for details how you can use the Assignment operators in PHP & MySQL.

Table 2  Assignment Operators in PHP & MySQL

Name of Operator

Symbol

Description

Use in PHP

Equal

=

Compare two variable for equal

$a = $b

Add & Equal (Addition)

+=

Add and then compare two variables for equal

$a += $b

Subtract & Equal (Subtract)

-=

Subtract and then compare two variables for equal

$ -= $b

Multiply & Equal (Multiplication)

*=

Multiply and then compare two variables

$a *= $b

Divide & Equal (Division)

/=

Divide and then compare two variables

$a /= $b

Modulus & Equal (Remainder)

%=

Take remainder and then compare two variables

$a %= $b

Difference between Assignment & Arithmetic Operators in PHP & MySQL:

If we check the assignment and arithmetic operators in PHP & MySQL. So just their using methods is

change from each other. But their result will be same. Check the table three for comparison of Assignment

and Arithmetic Operators in PHP and MySQL and see the Listing 3 for their use and same results.

Table 3  Note their results will be same

Assignment Operators Symbol

Arithmetic Operators Symbol

Use of Arithmetic Operators

Use of Assignment Operators

+=

+

$a + $b

$a += $b

-=

$a – $b

$a -= $b

*=

*

$a * $b

$a *= $b

/=

/

$a / $b

$a /= $b

%=

%

$a % $b

$a %= $b

Listing 1: Using Arithmetic Operators in PHP


<?

//using arithmetic operators in php & mysql

$a = 2;

$b = 2.5;

$sum = $a + $b;

$divide = $a / $b;

$modulus = $a % $b;

$multiply = $a * $b;

$subtract = $a - $b;

echo "<br>Addition of two values are: ".$sum;

echo "<br>Division of two values are: ".$divide;

echo "<br>Remainder of two values are: ".$modulus;

echo "<br>Multiplication of two values are: ".$multiply;

echo "<br>Subtraction of two values are: ".$subtract;

?>

Arithmetic

Figure 1   Table 4 Result of Arithmetic Operators in PHP & MySQL

Listing 2: Using Assignment Operators in PHP


<?

//using assignment operators in php & mysql

$a = 2;

$b = 2.5;

echo "<br>Addition of two values are: ".$a += $b;

echo "<br>Division of two values are: ".$a /= $b;

echo "<br>Remainder of two values are: ".$a %= $b;

echo "<br>Multiplication of two values are: ".$a *= $b;

echo "<br>Subtraction of two values are: ".$a -= $b;

?>

Assignment

Figure 2  Result of Assignment Operators in PHP & MySQL

Listing 3: Using Arithmetic & Assignment Operators in PHP


<?

//using arithmetic operators in php & mysql

$a = 2;

$b = 2.5;

$sum = $a + $b;

$divide = $a / $b;

$modulus = $a % $b;

$multiply = $a * $b;

$subtract = $a - $b;

print "<br><h3>ARITHMETIC OPERATORS IN PHP & MYSQL</h3><br>";

echo "<br>Addition of two values are: ".$sum;

echo "<br>Division of two values are: ".$divide;

echo "<br>Remainder of two values are: ".$modulus;

echo "<br>Multiplication of two values are: ".$multiply;

echo "<br>Subtraction of two values are: ".$subtract;

//using assignment operators in php & mysql

$a = 2;

$b = 3;

print "<br><h3>ARITHMETIC OPERATORS IN PHP & MYSQL</h3><br>";

echo "<br>Addition of two values are: ".$a += $b;

echo "<br>Division of two values are: ".$a /= $b;

echo "<br>Remainder of two values are: ".$a %= $b;

echo "<br>Multiplication of two values are: ".$a *= $b;

echo "<br>Subtraction of two values are: ".$a -= $b;

?>

Arithmetic&Assignemt

Figure 3  Result of Arithmetic & Assignment Operators in PHP & MySQL

Use of Increment & Decrement Operators in PHP & MySQL:

We use these two types of operators in PHP & MySQL for increase a variable value by 1 or

decrease by 1. And these operators are being used commonly in all programming languages as

now we will use them in PHP programming. Read the listing 4 for details and check the table 4 for

their full detailed information.

Table 4 Increment & Decrement Operators in PHP & MySQL

Name of Operator

Symbol

Description

Use in PHP

Post-Decrement

Returns variable, then decrease value of variable by 1.

$a–

Post-Increment

++

Returns variable, then increase value of variable by 1.

$a++

Pre-Decrement

Decrease value of variable by 1, and then return the variable

–$a

Pre-Increment

++

Increase value of variable by 1, and then return the variable

++$a

Listing 4: Increment & Decrement Operators in PHP & MySQL


<?

//using Increment Operators

$a = 0;

//result will be same as 0, and then increment by 1 so $a value will be 1

echo "Increased by one ".$a++;

echo "<br>Increased by one but first add by one then increased ".++$a;
//result will be 2, because $a = 1 + $a

//using Decrement Operators

$b = 0;

//result will be same as 0, and then decrement by 1 so $b value will be -1

echo "<br>Decreased by one ".$b--;

echo "<br>Decreased by one but first add by one then increased ".--$b;
//result will be -2, because $b = -1 -$b

?>

Increment&Decrement

Figure 4  Result of Increment & Decrement in PHP & MySQL

Starting Web Programming With PHP

PHP & MySQL Course:Starting Web Programming With PHP

In the previous lecture we learnt the errors and installation of the Xampp for Windows Server.

Lecture 3:Starting Web Programming With PHP

In this lecture we will learn the following topics.

  1. PHP Starting and Ending Tag
  2. Variables in PHP
  3. Output functions in PHP
  4. Saving the PHP programs in Xampp directory
  5. Execution of the PHP programs

PHP Starting and Ending Tag:

In PHP we use two types of Tags, one is PHP Tag and other is small Tag. We call the <php?> PHP Tag and we call the <??> Small Tag. In small tags we start and end the PHP programming from same thing. As you can see it <??>.

But in PHP Tag, we use <PHP to start the programming and when we end the PHP programming so we use?> to end it.  We commonly use the small tag in PHP programming. You can use both which one you like.

Variables in PHP:

There is a different way to use variables in PHP, but it is so simple. Because there are no any kind of data types or primitive data types. In PHP we declare variables when we need it. Otherwise we never declare variables. As we know the variables are used to store data in memory. So it is same usage of the variables in PHP. But when we declare variables in PHP, there is a way to declare any kind of variable in PHP. We use $ before the variable name i.e. $name. So the $name is a variable in PHP but we don’t know what is its type?

As I told you that, there is no any data type. Which we use in PHP to declare the variables. As we commonly use the data types in C++ or Java. For example: we use the following primitive data types in C++ or Java

 

  1. Char          For the declaration of character variable
  2. Int             For the declaration of Integer variable
  3. String        For the declaration of String variable
  4. Float          For the declaration of decimal variable

Like this we use few more. But the question is, how we can use the variables and also how we can declare them in PHP. For more details check the Listing 1

Listing 1: Declaration of Variables


<?php

$name = “TrustInGeeks”;               //String variable

$section = ‘A’;                               //Character variable

$gpa = 3.25;                                   //Float variable

$number = 123345;                         //Integer variable

?>

As you can see the listing 1, in which I used four variables. And these all variables are fully different from each other. The $name variable is a String variable which is storing the name of the website. The $section variable is a character (char) variable which is storing a letter. The $gpa is a float variable which is storing the decimal numbers. And in last the $number is an integer (int) variable which is storing the whole numbers.

So we can say it, the way of using the variable and also their declaration is fully changed from the C++ and Java Programming Languages. As you can see we didn’t use any kind of primitive data type for those four variables.

Output Functions in PHP:

In PHP we use few methods to display the result of our programming of PHP. We use Echo and Print to display in PHP. The Echo is used to display single or multiple strings. But the Print is used to display a single string and also return 1. So it’s mean that Echo never returns 1 but Print does. And we commonly use Echo so I prefer to use Echo. Because we use to display multiple strings at a time. Check the Listing 2 for details.

Listing 2: Echo and Print usage


<?

Echo “Welcome to Trust In Geeks”;

Print “Where you Learn.”;

?>

We can also use variables with them to display the stored data of variables. So let’s we use the listing 1 variables in listing 3. Now we will use echo and print to display data from the variables.

Listing 3: Echo and Print with Variables


<?

$name = "TrustInGeeks";               //String variable

$section = 'A';                               //Character variable

$gpa = 3.25;                                   //Float variable

$number = 123345;                         //Integer variable

//displaying variable data using echo

Echo "The website name is ".$name;

Echo "My section is ".$section;

Echo "My current Grade Point Average is ".$gpa;

Echo "My number is ".$number;

//displaying variable data using print

Print "You can Learn everything at ".$name;

Print "Try to get ".$section;

Print "You got ".$gpa;

Print "It is your number ".$number;

?>

As you can see we used these two functions to display data. So you can use them as I used. And there difference is clear when you will execute them.

Saving the PHP programs:

Now let’s save the PHP programs and execute them. We use .php extension to save them, it is common to use now a days in PHP programming. And as in last lecture I taught you how to install the Xampp Windows Server. We will save all PHP programs which extension is .php in this directory C:xampphtdocs. We will save all our web pages which are developed in PHP & MySQL at htdocs folder. Which is in Xampp.

And when we need to display or execute the programs of PHP. We will use this link http://localhost to execute the PHP programs. But after the localhost we will use forward slash and then we will write the program name. i.e. http://localhost/first.php as you can see in this link. There is a program name which name is first.php. It will execute after writing it in browser’s address bar.

As I saved the listing 3 program and then execute it. See the Figure 1 for details.

Lecture 3

Figure 1   Output of Listing 3 Program

Summary:

In this lecture we learnt the basic syntax of PHP programming. In basic syntax we learnt the PHP tags starting and ending tags and their uses. We learnt variables declaration and we also confirmed that there is no any kind of data type in PHP. So it is very easy to use the variables in PHP programming during the website development using the PHP & MySQL.

We also learnt the two functions for display data and we use them with variables. We will learn them more in next lectures because there are some more way of use for them. As you can see we used dot operator with variables. We can use variables with them to display data but without dot operator.

PHP Global Variables and HTML Form

We learnt to save the PHP programs and execute them. And as you can see the result of first program in Figure 1.

Xampp Installation – Setting up the Environment for PHP programming

PHP & MySQL Course:

In the previous lecture we just learnt something about the basics. And also some important things which will be need us during this course.

Lecture 2:

In this lecture we will install three things which are most important for our course.

  1. Xampp For Local Host Server
  2. Navicat For MySQL
  3. Notepad++ For PHP & MySQL IDE

Xampp For Local Host Server:

First of all we should know about the Xampp Windows Server. The important job of the Xampp is to execute PHP & MySQL programs on the browser. Let’s we start the installation of the Xampp Windows Server step by step. Follow the pictures for your understand. First of all download the Xampp Application for your windows. As I mentioned the website in my previous lecture from which you can download it. After download double click on the executable file of Xampp or right click on it and then click on the Run as Administrator from the drop down list. After following this you will see the window as in Figure 1. XammpWindowsServerInstallation-1 Figure 1   Confirm the Language which you can understand then click on OK In Figure 1, it is a chance to select your desired language. We commonly use English and it is built-in selected Language. You can change it now and then press OK button to proceed the installation. XammpWindowsServerInstallation-2 Figure 2    This window will check the Graphics and Resolution of your Computer. In Figure 2, it is confirming the version of your windows as 64bit or 32bit. The Xampp is available commonly 32bit version for all type of Operating systems. So don’t worry about it. It will work correctly on your computer after installation. Just press OK button and process. XammpWindowsServerInstallation-3 Figure 3   Setup Wizard Confirmation… Figure 3, it is showing the version of the Xampp Setup Wizard. Confirm it and press Next button and proceed for the next window. Don’t waste your time on pressing Cancel button. XammpWindowsServerInstallation-4 Figure 4   Directory of the Xampp Windows Server In Figure 4, it is giving you a Golden chance to change the directory of your Xampp Windows Server. If you changed it now so it will be easy to work with it. But you don’t do it now, so it will be difficult of you to change it later. I commonly suggest you to select a different local drive for your Xampp Windows Server as local Drive D, E, etc. Because if by chance or suddenly your OS drive is corrupt so you will lost your all data which you done. XammpWindowsServerInstallation-5 Figure 5 Services which you want to use from the Xampp Windows Server In Figure 5, you can select the Services which you want to use. There are three services which we use them commonly. They are, Installation Apache as Service, Installation MySQL as Service, and Installation Filezilla as Service. You should select these three services for your Xampp windows server best performance. XammpWindowsServerInstallation-6 Figure 6 Selected services for Xampp Windows Service In Figure 6, you can see the services which I selected for the Xampp windows service best performance. Check your selection and confirm it. Then press the Installation button to proceed. XammpWindowsServerInstallation-7 Figure 7    Xampp Windows Server Installation In Figure 7, now you can see in this figure the installation is begin. So don’t do anything just wait for the normal mode where you can press the Next button, then proceed forward. XammpWindowsServerInstallation-8 Figure 8 Confirming the Xampp Windows Server Installation In Figure 8, now it is confirmed that the Xampp Windows Server is installed on your local system without any error. So without any hesitation click or press the Finish button and proceed. XammpWindowsServerInstallation-9 Figure 9  confirming the Ports for Running the Xampp Windows Server In Figure 9, it is confirming the ports. As you can see there is a message of failed. It means that the default Ports are busy at this time may be there is any other application which is using them. So don’t worry for this error just follow the installation step by step. In Figure 9, press OK button to proceed. XammpWindowsServerInstallation-10 Figure 10  Installation Finished Successfully In Figure 10, you can see there is a message. In which you can see the installation is finished successfully. So your Xampp Control Panel is fully ready to use. Press OK to proceed. XammpWindowsServerInstallation-11 Figure 11   Congratulations In Figure 11, it is congratulating us for the Xampp Windows Server installation and being ready for the use. Click on Yes button to use the Xampp Control Panel first time. XammpWindowsServerInstallation-12 Figure 12  Telling the Current Directory Error In Figure 12, it is confirming the current Directory for your Xampp Control Panel. It sometimes not run from the current directory so we use the installation directory where we installed the Xampp Windows Server. So just go there and double click it from the directory. XammpWindowsServerInstallation-13 Figure 13  Xampp Control Panel In Figure 13, as you can see now we are using the Xampp Control Panel first time on Local computer. Here now check the services which you check at the services confirmation. Here it is showing them as Modules and they are checked now if anyone is not start so click on it to start it. XammpWindowsServerInstallation-14 Figure 14  All services are running In Figure 14, as you can see. Which services we had selected last time. So there were some are not started automatically in Figure 13. So now they are running successfully in Figure 15. XammpWindowsServerInstallation-15 Figure 15   Browser and Main or Home page of Xampp Windows Server In Figure 15, you can see there is a Home page on browser. So for this page use this link http://localhost, and then press Enter. You can also select your desired language from this page. So select that language which you can understand easily and start the using of Xampp Windows Server. XammpWindowsServerInstallation-16 Figure 16  Use all features of the Xampp for Windows Server In Figure 16, it is showing us the menus of the Xampp for Windows Server. Which you can use freely for your website development. So there are lot of modules and menus are waiting for you. XammpWindowsServerInstallation-17 Figure 17  confirmation and Finalization In Figure 17, it is confirming the installation and from which you can see the details about your installed Xampp Windows Server. XammpWindowsServerInstallation-18 Figure 18  PHP MyAdmin Home page In Figure 18, here you can use the MySQL facility. So you can easily use the MySQL to create and insert data to your website. Note: the installation of the Xampp for the windows server is little bit confusing and also difficult. That’s why I thought to tell you the complete installation of the Xampp. The downloading and installation of the Notepad++ and Navicat for MySQL is fully easy just click next and next buttons. Then they will be fully and successfully installed on your Local machines.

Summary:

In this lectures we discussed the installation of the Xampp for Windows Server step by step from the scratch and we also discussed the problem which can be occur during the installation.

Static and Dynamic webpages

PHP & MySQL Course:

I am going to start a Dynamic Website developing course, in which you will learn each and every thing about PHP & MySQL in step by step procedures.

Lecture 1:

It is the first lecture in which we learn the importance of the PHP & MySQL. PHP stands for   PHP Hyper Text Preprocessor. As we know that there are two types of website Web Pages.

  1. Static Web Pages.
  2. Dynamic Web Pages.

Static Web Pages:

In Static Web Pages, the data is Static. It does not change, because it is not connected with the database.

Dynamic Web Pages:

In Dynamic Web Pages, the data is Dynamic. And the retrieve from the database during the time of browsing. And it is very secure, if we design Dynamic web pages. Because our data will communicate with database and attacking on the servers is not a piece of cake.

Integration Development Environment (IDE) For PHP & MySQL:

If we want to start website designing with the help of PHP & MySQL. So we just need to use a simple text editor for PHP & MySQL. We can use other IDE for PHP & MySQL programming, as if we want use an artificial intelligence. So we need to use Dreamweaver, Netbeans etc. There are many other IDE which can be used for the PHP & MySQL programming in a Dynamic Website development.

For your learning purpose you should use a simple IDE, in which you feel to learn and keep them in your mind. Don’t forget them because they will need to be used on any time and any place.

Server & Database For PHP & MySQL Programming:

As we know that PHP is a sever side programming language. And it needs a server which hosts the Dynamic Web Pages which are programmed with PHP & MySQL. And we also need a database application for the MySQL to communicate with the database during the browsing of Dynamic Web Pages. In this PHP & MySQL course we will use two application which are being used mostly by PHP & MySQL programmers i.e. Xampp Apache for PHP and Navicat for MySQL.

Links for PHP & MySQL Applications for the Course:

For Xampp use this link to download it, http://www.apachefriends.org. And use this link for Navicat MySQL use this link to download it, http://www.navicat.com/download

NOTE:

We can also use the Xampp for the MySQL, because Xampp is that application which provide us everything, which could be needed during the development of a dynamic website. But the use of the Xampp for Database is little bit difficult. And if we use the Navicat for MySQL so it will be much easier than the Xampp as a database application.

Local host server:

There is a problem with new website developers, it happens usually at the beginning when they start from the scratch of the website development. That is hosting, we need a place where we can host our website and the visitors will able to browse the website. According to this course we need a server which host our dynamic web pages, and we can visit them fully free. So that is Xampp which we discussed it previous paragraphs. It will create our local machine or Local PC as a local Host Server. And we will use it to save our dynamic web pages directly to its directory and we can access our dynamic web pages which we designed.

And at the run time or during the browsing of the web pages we will know the errors and use of the website. In the designing or development of anything, every time we need to take care of the users or visitors. Because we know each and every thing about the system. How it will work and for which purpose it is going to be design.

FTP (File Transfer Protocol):

When we fully designed the website. And when we feel that now it is the time to upload the website on the Internet. There are few steps when we want to upload our website;

  1. Buy a Hosting from any good Server which you think that it is best for your website.
  2. Register a Domain (Name of the Your Website).

After these two things, now we can use the FTP to upload our website on the server which we bought for our website purpose. We will read these all things in details but after the completion of the PHP and & MySQL programming course.

Summary:

In this lecture, we learnt about the basics of the website development. As we just reviewed the basics of the Dynamic Web site development. We discussed the types of Web Pages, IDE for PHP & MySQL etc. And also we reviewed the Local Host server and also FTP.

These things are the basics of the Dynamic Website development. We should know about these things. As you heard about the CMS (Content Management System) we use them for Dynamic Website creation. And which things we discussed in this lecture these all things are need to design a CMS for your own purpose. If you read all the lectures of this course so you will be able to design your own CMS to use it for your future Website development in a simple and easy way.

One thing I should tell you in the start of this course, I will use Notepad++ as IDE in this course. Because if you are new so you should as simple IDE as your programming environment, from which you feel to learn and remember every function. If you use a High Quality IDE as there are Dreamweaver and Netbeans for the PHP programming so you will use artificial intelligence of them. And you will not learn anything. You will be used to of those IDE. Because these IDE provide you many things which you need for Website development.

I hope you learn something from the first lecture and now you should set your mind for the Installations of the Environments. Because now we will download and install the applications which we discussed in this lecture.