Introduction to PHP

introduction to php

Introduction to PHP

The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is a server-side scripting language, like ASP .PHP scripts are executed on the server.PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) .PHP is an open source language.PHP is basically used for developing web based software applications.

PHP started out as a small open source project that evolved as more and more people found out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994.

  • PHP originally stood for "Personal Home Page".
  • PHP is a recursive acronym for "PHP: Hypertext Preprocessor".
  • PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.
  • It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.
  • PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The MySQL server, once started, executes even very complex queries with huge result sets in record-setting time.
  • PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4 added support for Java and distributed object architectures (COM and CORBA), making n-tier development a possibility for the first time.
  • PHP is forgiving: PHP language tries to be as forgiving as possible.
  • PHP Syntax is C-Like.

Common uses of PHP
  • PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
  • PHP is a recursive acronym for "PHP: Hypertext Preprocessor".
  • PHP can handle forms, i.e. gather data from files, save data to a file, through email you can send data, return data to the user.
  • You add, delete, modify elements within your database through PHP.
  • Access cookies variables and set cookies.
  • Using PHP, you can restrict users to access some pages of your website.
  • It can encrypt data.
Characteristics of PHP

Five important characteristics make PHP's practical nature possible −

  • Simplicity
  • Efficiency
  • Security
  • Flexibility
  • Familiarity
Features

Five important characteristics make PHP's practical nature possible −

  • In PHP there is no need to specify data type for variable declaration. Rather, it can be determined at the time of execution depends on the value of the variable. So that, PHP is called as loosely typed language.
  • PHP provides cross platform compatibility, unlike some other server side scripting language.
  • PHP has set of pre defined variables called superglobals which will be start by _. Some of the examples are, $_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER and etc. So, any variable except superglobals, that are start with _ will cause error.
  • PHP programming structure includes variable variables; that is, the name of the variable can be change dynamically.
  • This language contains access monitoring capability to create logs as the summary of recent accesses.
  • And then, it includes several magic methods that begins with __ character which will be defined and called at appropriate instance. For example, __clone() will be called, when the clone keyword is used.
  • Predefined error reporting constants are available to generate a warning or error notice. For example, when E_STRICT is enabled, a warning about deprecated methods will be generated.
  • PHP supports extended regular expression that leads extensive pattern matching with remarkable speed.
  • And then, properties like, nowdocs and heredocs are used to delimit some block of context which should not be sent for parsing.
  • Since PHP is a single inheritance language, the parent class methods can be derived by only one directly inherited sub class. But, the implementation of traits concept, reduce the gap over this limitation and allow to reuse required method in several classes.

"Hello" Script in PHP
To get a feel for PHP, first start with simple PHP scripts. Since "Hello, World!" is an essential example, first we will create a friendly little "Hello, World!" script.
As mentioned earlier, PHP is embedded in HTML. That means that in amongst your normal HTML (or XHTML if you're cutting-edge) you'll have PHP statements like this –

<html>
<head>
<title> Hello World</title>
</head>
<body>
<?php echo “Hello!”; ?>
</body>
</html>
It will produce following result −
Hello!

If you examine the HTML output of the above example, you'll notice that the PHP code is not present in the file sent from the server to your Web browser. All of the PHP present in the Web page is processed and stripped from the page; the only thing returned to the client from the Web server is pure HTML output.

Most Basic PHP Syntax A PHP scripting block always starts with '<?php' and ends with '?>' . A PHP scripting block can be placed anywhere in the document.

PHP Variable Declaration
All variables in PHP begin with the $ sign.
Ex.
<?php
$my_variable = 20;
$my_variable2 = “Hello World”;
?>

Variable Rules
You have to follow some rules when naming a variable:
  1. A variable name cannot contain spaces.
  2. A variable name must start with a letter or an underscore ( _ )
  3. A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )

String Variables
<?php
$my_string = “Hello everybody”;
echo $my_string;
?>

strlen() function
The PHP strlen() used to get length of string

<?php
echo strlen("I’m sorry dave, I’m afraid I can’t do that.");
?>

The result of the statement will be a number (the length of the string including space and signs, not only the characters), in this case the number 43.

strpos() function
The PHP strpos() function is used to search for a character or string within a string. If the character is found the strpos() function will return the position of the first match. If strpos() can’t find the character in the string, it will return FALSE.

<?php
echo strpos(“Open the pod bay doors please, HAL”,”HAL”);
echo strpos(“Open the pod bay doors please, HAL”,”H”);
?>

The position of the string “HAL” in the whole string is position 31 (the space before the string HAL.) The reason that it is 31 (space) and not 32 (the character H) is that the first position in the string is 0, and not 1.

Concatenation Operator
PHP has only one string operator. If you want to put two string values together you can use the concatenation operator (.)

<?php
$str_one=”Hello“;
$str_two=”World!”;
echo $str_one . “ “. $str_two;
?>

The result will be “Hello World!”.

Get In Touch

HAV Solutions
First Floor-305, Raghuleela Mega Mall, Behind Poisar Depot, Kandivali West, Mumbai, Maharashtra 400067, India

Phone : +91-9819237992
Email : contact@havsolutions.com