What is PHP?
It is an open source scripting language which can be used to develop the dynamic website easily. People are using PHP to develop small and big e-commerce applications.
History of PHP
- In 1994 the development of PHP was started by Rasmus Lerdorf
- On June 8, 1995, it was released publicly
- In 1997, Zeev Suraski and Andi Gutmans rewrote the parser.
- In 1998, PHP 3 was officially launched
- In 1999 Suraski and Gutmans producing the Zend Engine
- On May 22, 2004, PHP 4 was released and it was powered by Zend engine 1.0
Different features in PHP
- Open source, free to download and use
- Cross platform
- Compatible with any server
- Supports popular databases
- Easy to learn
Data types supported by PHP
Following data types are supported by PHP:
- Integer: It is stored as signed integers with 32 bits, with a range of -2,147,483,648 and 2,147,483,647
- Float: It is stored as IEEE floating point number with 64 bits.
- String: It is a sequence of 8-bit characters.
- Boolean: It has two literal values: 'true' and 'false'
- Array: It store an ordered map of pairs of keys and values.
- Object: It stores an instance of class.
- Resource: It is for storing file handling, or database connection.
- Null: It has only value null.
Every code in PHP starts with <?php and ends with ?>
If you want to print "I am learning php!" using PHP, then either use print or echo command, both are used to print anything on the screen, so type either :
Example 1:
<?php
print("I am learning PHP");
?>
Example 2:
<?php
echo("I am learning PHP");
?>
Output:
Above written codes will display on the screen :
I am learning PHP
print and echo are almost similar besides that they have few differences like:
Speed: echo is slightly faster than print
Parameters: echo allows more than one parameter whereas print does not, e.g.:
<?php
print "Hello","world";
?>
will generate error, whereas:
<?php
echo "Hello","world",1,2,3;
?>
will gives the output as:
Helloworld123
Expression: print works like a function and returns true value after successfully executed.
PHP Cookies
Cookies are little packets of information that are stored locally on a computer system when you visit a website that utilizes them. Cookies are part of HTTP header, so cookies must be set before sending any value to the browser (i.e. before html tag).
Cookies can be used to store identifiable information about a user on their computer so that the website can pick up it later, even after your computer has been turned off, and you may continue from where you started.
Cookies can be used to store identifiable information about a user on their computer so that the website can pick up it later, even after your computer has been turned off, and you may continue from where you started.
Syntax of cookie is :
setcookie(name, value, expire, path, domain)
An example of this is with member systems and forums, when you are offered the "Remember me" option. With that, the website stores your username on your computer, and it can then find it when you log on to the website next, and enter your username automagically in the textbox.
Cookies are pretty easy to use, but you must remember that some people may not like the idea of having little pieces of information get stored on their computer, as the consequences other people might come to know about the secret data.
Example 1:
<?php
setCookie("Name","opensourzesupport");
if(isset($_COOKIE['Name']))
{
echo $_COOKIE['Name'];
}
else
{
echo "Cookie set, please refresh the page";
}
?>
Output:
opensourzesupport
Example 2:
<?php
setCookie("Name","opensourzesupport");
SETCOOKIE("ADDRESS","NEW DELHI");
SETCOOKIE("TIME",date("M d y"));
if(isset($_COOKIE['Name']))
{
echo $_COOKIE['Name'];
}
else
{
echo "Cookie set, please refresh the page";
}
print_r($_COOKIE);
?>
Output:
opensourzesupportArray ( [Name] => opensourzesupport [ADDRESS] => NEW DELHI [TIME] => Nov 09 09 )
Example 3 (You can set the time of expiration):
If you want to set the time of expiration to 1 hr. then do as follows:
<?php
setCookie("Name","opensourzesupport");
SETCOOKIE("ADDRESS","NEW DELHI");
SETCOOKIE("TIME",date("M d y"),time()+60);
if(isset($_COOKIE['Name']))
{
echo $_COOKIE['Name'];
}
else
{
echo "Cookie set, please refresh the page";
}
print_r($_COOKIE);
?>
Output:
opensourzesupportArray ( [Name] => opensourzesupport [ADDRESS] => NEW DELHI [TIME] => Nov 09 09 )
Certainly the output is not different from the previous one, but the values will not exist in the cookie more than one hour.
Example 4 (How to delete a cookie):
<?php
SETCOOKIE("user","",time()-3600);
if(isset($_COOKIE['Name']))
{
echo $_COOKIE['Name'];
}
else
{
echo "Cookie set, please refresh the page";
}
print_r($_COOKIE);
?>
File Manipulation in PHP
In PHP, we can do many things with file, like creation of file, update of file, deletion of file etc. One of the important work is writing to a file, following example will illustrate how to open a file and write into it, in addition we will learn how to open a file and read the The following examples will let you know about the most basic form of file handling.
Example 1 (Let's assume that we have created a .txt file inside C:\wamp\www\php directory, where we will write the .php file):
<?php
$file=fopen("Newest.txt","r");
while(!feof($file))
{
echo (fgets($file));
}
?>
Output:
In PHP, we can do many things with file, like creation of file, update of file, deletion of file etc. One of the important work is writing to a file, following example will illustrate how to open a file and write into it. In addition we will learn how to open a file and read the The following examples will let you know about the most basic form of file handling.
Example 2 (If you want to create the file, we will use 'w' mode instead of 'r'):
<?php
$file=fopen("Newest.txt","w");
$str="This is same file with different text";
fputs($file,$str);
$file=fopen("Newest.txt","r");
while(!feof($file))
{
echo fgets($file)."<br/>";
}
?>
Output:
This is same file with different text
Example 3 (If you want to append some text into a file change the mode into 'a'):
<?php
$file=fopen("Newest.txt","a");
$str="This is same file with extra text which has been appended";
fputs($file,$str);
$file=fopen("Newest.txt","r");
while(!feof($file))
{
echo fgets($file)."<br/>";
}
?>
Output:
This is same file with different textThis is same file with extra text which has been appended
PHP Database
PHP Database Creation
A simple website is made of the following technologies:
i) HTML/CSS: HTML & CSS is used to get the user interfaces, and a pretty look for the web site.
ii) PHP: PHP processes the forms and other dynamic wizardry.
iii) Database: We need to store several data e.g.- user list, product information etc. There are different databases are available in market, we need to choose any specific database according to our need.
Many more technologies could be used in web site development like: JavaScript etc. for validation checking.
Now in this tutorial you will get to know about databases, tables, query and other related things like how to access databases using PHP etc. Database: In general a database is a collection of logically related files or records. A database could be a pile of structured information, a program (e.g. MySQL, or Oracle) which manages those structured information, or the computer on which the program runs. Table: Table is a collection of rows and columns. Columns keep only one specific (e.g. First-Name, Last-Name, Age etc. ) data where rows are the collection of columns (e.g. Sachin, Tendulkar, 37 etc.). Tables store data in a grid like pattern of columns and rows. Query: In database a query is a set of specific words which are collectively used to retrieve the desired information.
In the following PHP Database tutorial we will study various things of PHP Database, like: A) MySQL
i) Downloading and installing MySQL server.
ii) Creating Database
iii) Creating Table
iv) Inserting Information
v) Retrieving Values
A simple website is made of the following technologies:
i) HTML/CSS: HTML & CSS is used to get the user interfaces, and a pretty look for the web site.
ii) PHP: PHP processes the forms and other dynamic wizardry.
iii) Database: We need to store several data e.g.- user list, product information etc. There are different databases are available in market, we need to choose any specific database according to our need.
Many more technologies could be used in web site development like: JavaScript etc. for validation checking.
Now in this tutorial you will get to know about databases, tables, query and other related things like how to access databases using PHP etc. Database: In general a database is a collection of logically related files or records. A database could be a pile of structured information, a program (e.g. MySQL, or Oracle) which manages those structured information, or the computer on which the program runs. Table: Table is a collection of rows and columns. Columns keep only one specific (e.g. First-Name, Last-Name, Age etc. ) data where rows are the collection of columns (e.g. Sachin, Tendulkar, 37 etc.). Tables store data in a grid like pattern of columns and rows. Query: In database a query is a set of specific words which are collectively used to retrieve the desired information.
In the following PHP Database tutorial we will study various things of PHP Database, like: A) MySQL
i) Downloading and installing MySQL server.
ii) Creating Database
iii) Creating Table
iv) Inserting Information
v) Retrieving Values
No comments:
Post a Comment