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.
- PHP Starting and Ending Tag
- Variables in PHP
- Output functions in PHP
- Saving the PHP programs in Xampp directory
- 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
- Char For the declaration of character variable
- Int For the declaration of Integer variable
- String For the declaration of String variable
- 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.
Figure 1 Output of Listing 3 Program
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.
We learnt to save the PHP programs and execute them. And as you can see the result of first program in Figure 1.