Introduction to PHP

Introduction to PHP

  • PHP
  • 6 mins read

What is PHP?

PHP is an acronym for "PHP: Hypertext Preprocessor". Previously it was called as Personal Home Page. Actually, It was created by Rasmus Lerdorf in 1994.

PHP is a server-side scripting language. Like any other scripting language, PHP allows developers to build logic into the creation of web page content and handle data returned from a web browser. PHP also contains a number of extensions that makes it easy to interact with databases, extracting data that will be displayed on a web page and storing information entered by a website visitor back into the database.

PHP consists of a scripting language and an Interpreter. Like other scripting languages, PHP enables web developers to define the behavior and logic they need in a web page. These scripts are embedded into the HTML documents that are served by the web server. The interpreter will take the form of a module that integrates into the web server, converting the scripts into commands the computer then executes to achieve the result defined in the script by the web developer.

How does PHP work?

To develop an understanding of how PHP works it is helpful to first explore what happens when a web page is served to a user's browser.

when a user will visit a website or click on a link on a web page the browser sends a request to the web server hosting the site for a copy of the web page. Then the web server receives the request, find the corresponding web page file on the file system and sends it back, over the internet, to the user's browser.

Typically the web server does not pay any attention to the content of the file it has just transmitted to the web browser.

These days a web page is likely to consists of HTML, XHTML, and JavaScript. The web browser contains code that tells it what to do with these type of content. A web browser knows absolutely nothing about any PHP script that may be embedded in an HTML document. If a browser was served a web page containing PHP it would not know how to interpret that code. Given that a web browser knows nothing about PHP in a web page, then clearly something has to be done with any PHP script on the web page before it reaches the browser. That is where PHP preprocessing module comes in. The module tells the web server that when a page is to be served which contains PHP script that it is to pass that script to the PHP pre-processing module and wait for the PHP module to send it some content to replace that script fragment.

Actually, The PHP processing module understands PHP, executes the PHP script written by the web developer, creates output that the browser will understand.

Why we will learn PHP?

  1. It is easy to learn and easy to understand
  2. It will run on many platforms such as Windows, Linux, Unix etc
  3. It  supports a large range of databases
  4. PHP can be downloaded from the official website(www.php.net) freely
  5. Today PHP can run on almost all servers such as Apache, IIsc etc.

Let us an example of PHP code embedded into an HTML code:-

<html>
<head>
<title>Happy New Year 2019</title>
<body>
<?php
echo "<h1>well come to Kolkata</h2>";
?>
</body>
</html>

Description: Here <?php and ?> markers that will specify where the embedded PHP script is starting and ending.When the web server finds it then will send it to the PHP module. The PHP module interprets it, convert it to HTML and sends it back to the web server. The web server sends the following to the browser:

<html>
<head>
<title>Happy New Year 2019</title>
<body>
<h1>well come to Kolkata</h2>
</body>
</html>

Once loaded into the browser, it is rendered just like any other web page.

PHP Syntax

A PHP script can be inserted anywhere in a document. A PHP script will start with <?php and will end with ?> such as:

<?php
//codes
?>

The default file extension of PHP file is .php. A file can contain HTML tags and some PHP scripting codes.

PHP Variables

PHP variable is like a container which can store information.

How will declare a variable?

Every variable in PHP will be started with $ sign followed by the name of the variable. A variable can be declared anywhere within the php script.

<html>
<body>
<?php
$a=90;
?>
</body>
</html>

here after the execution of  first statement($a=90;)$a will contain the value 90.

Rules of PHP Variable

  • Every variable will be started with $ sign followed by the name of the variable.
  • Every variable name will be started with a letter or _(underscore character).
  • PHP variable can contain _(underscore) character.
  • Variable names are case sensitive($kol and $KOL are two different variables).
  • Variable name can contain digits(0-9).
  • ariable name can not contain space,#,&,.,-,@ etc.

PHP Data Types

Actually PHP variable can contain data of different data types.

PHP Supported Data Types:

  • String
  • Integer
  • Float(floating point numbers)
  • Boolean
  • Array
  • Object
  • Null
  • Resource

PHP String

Actually, a string is a sequence of characters. It is enclosed using single and double quotes.

Example:

<html>
<body>
<?php
$a="well come to Kolkata";
$b='well come to kolkata';
echo $a;
echo $b;
?>
</body>
</html>

Hereafter the execution of two statements $a and $b will contain the text "well come to Kolkata" and through echo statement, the string "well come to Kolkata" will be displayed on the web page for two times.

Integers

An integer can contain the whole numbers such as 1,-2 etc. Integer can be specified in decimal(base 10), hexadecimal(base 16), octal(base 8) or binary(base 2) notation, optionally preceded by a sign(- or +).

To use octal notation. The number is preceded by 0(zero), to use hexadecimal notation..the number is preceded by 0x.To use binary notation the number is specified by 0b.

Example:

<html>
<body>
<?php
$a=98; //decimal number
$b=056; //octal number
$c=0x56; //hexadecimal number
$d=0b111;//binary number
echo $a;
echo $b;
echo $c;
echo $d;
?>
</body>
</html>