Introduction to Creating Dynamic Web Pages:
What is PHP?
PHP (Hypertext Pre-processor) is a web development language. You can download it from php.net. PHP is a server side programming language which is embedded in html. In html we can design our pages like creating links, inert images in a web page, creating tables etc. but there is no such type of function to add two numbers and build a connection to the Database, so there is PHP which do all these functions easily.
PHP is helpful for creating dynamic web pages. The difference between dynamic and static is that the structure and contents of dynamic web pages are changed (through the back end programming) while static pages are fixed whenever programmer does not change it.
Through the PHP you can control large databases and PHP supports almost all databases for example, MS-SQL, MySQL, PostgreSQL, SQLite, Access, and Oracle etc. You can develop PHP forms, e-commerce sites, photo galleries, Site Layouts, Multimedia Websites, News, Content Management System, and File Management System etc. in limited time.
We start PHP programs with <?php and close with ?> within these tags we write or print anything which we want. Also you can use PHP short tags which are <? //some code here?>.
Listing 1: Output using "echo"
<? php echo “This is my first program in php”; ?>
OUTPUT Listing 1:
This is my first program in PHP
This is simple printing some text. PHP syntax is very easy and its syntax has similarities with C++, Java and Perl. If you have work any language from them, then there is no difficulties for you to learn PHP. As PHP is server side programing language so there must be installed xampp/wampp in which local server exist ( apache ) also mysql database which is required to store our PHP data .We will talk later about mysql in detail. After install xampp go to xampp control panel to start your local server then you can run your PHP programs. The import thing is that you must save your PHP program in xampp->htdocs folder.
What is MySql And Why we use it with PHP
MySQL is a very fast Relational Database Management System (RDBMS) which is fully free to use. It give us a database in which we can store our huge amount of data and also provide us a proper and easy way to search and retrieve data very fast. It give us also some security in which the different user have different privileges to access the data. MySQL works on Structure Query Language (SQL) which is a standard for all databases. MySQL is the world best open source database, which has won the Linux Journal Readers Choice Award many time, therefore mostly we use MySQL with PHP.
Basic programming in PHP:
Let we start PHP from some basic programs, you must save your PHP programs in C drive there is program files and then xampp in xampp there is htdocs i.e. folder C ->programs folder -> xampp ->htdocs
In Listing 2, program we write first of all ‘<html>’ tag which is starting tag of html script and then ‘<head> ‘ this is use basically for like header in which we write title of page like ‘<title> This is title </title>’, then we started pages body section ‘<body>’ , in which our PHP code is exist which starts from <?php and closed at ?> and then close </body> and </html>.
<html> <head> <title> This is title </title> </head> <body> <?php echo “ This is the php tag within html”; ?> </body> </html>
How to use Variables in PHP:
In PHP there is no variable type just we define variable name with start $ sign and then any name. We can write variables name (A-Z, a-z,_,0-9), but variables name cannot be started with number and a white space between words. Let us to see some examples of variables.
Like C++, Java at the end of statement we use ‘; ’. The above variables are declared now we make some variables and at the time of declaration we assign it value.
$y; $y_long_variable_name; $_13713; $sheela;
$univasity = “Laknoohish”; <?php echo $university. ”<br>”; // <br> uses for new line. ?>
OUTPUT Listing 4:
PHP has no any data type we just define variables with starting with $ sign and then any name of variable.
Let we see more examples of variables.
<?php $var1 = 137; $var2 = 7.13; $var3 = “sheela”; $var4 = ‘ SN ’; $sum = $var1 + $var2 ; // add $var1 and $var2 values = 137 +7.13 echo “ the sum is : ” . $sum ; echo “ <br> the values of other variables: ”.$var3 . “ ” . $var4; ?>
OUTPUT Listing 5:
the sum is : 144.13
the values of other variables : sheela SN
Also we can find our variables data type by using functions, before for that you just declare a variable and then we assign it different data types, let we see it by using example.
<? $var_name; // there is no value in our variable echo “The data type of our variable is ” .gettype ($var_name) . “ <br>”; $var_name = “sheela SN”; echo “ The data type of our variable is ” .gettype ($var_name) . “ <br>”; $var_name = ‘N’; echo “ The data type of our variable is ” .gettype ($var_name) . “ <br>”; $var_name = 137.13; echo “ The data type of our variable is ” .gettype ($var_name) . “ <br>”; $var_name = true; echo “ The data type of our variable is ” .gettype ($var_name) . “ <br>”; ?>
OUTPUT Listing 6:
The data type of our variable is NULL
The data type of our variable is string
The data type of our variable is char
The data type of our variable is double
The data type of our variable is Boolean
In this example we declare a variable $var_type and by using gettype function which return us data type of variable which we have assign at run time. At the every statement of assignment the variable value and its type changed.
Now there is another function which sets the variable type at the run time which is settype function how we use it, let see an example.
<php $var = 100; echo “ Data type of my variable is :” . gettype($var). “<br>”; settype($var, “string ”); echo “ the variable is now a :” . gettype($var); echo “the variable type is now a :” . gettype($var); ?>
OUTPUT Listing 7:
Data type of my variable is: integer
The variable type is now a: string
Here we declare a variable $var and we assign it integer value of 100. And then in second statement we set our variable to a string by using settype function. Now the current type of our variable is a string.