Post-1 (PHP) PHP Introduction - NetwaxLab

Breaking

Facebook Popup

BANNER 728X90

Friday, March 25, 2016

Post-1 (PHP) PHP Introduction



PHP is a Server-Side Scripting Language designed for web development. Originally created by Rasmus Lerdorf in 1994, the PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive backronym PHP: Hypertext Preprocessor.


PHP is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

Instead of lots of commands to output HTML (as seen in C or Perl), PHP pages contain HTML with embedded code that does "something" (in this case, output "Hi, I'm a PHP script!"). The PHP code is enclosed in special start and end processing instructions <?php and ?> that allow you to jump into and out of "PHP mode."

The mascot of the PHP project is the elePHPant, a blue elephant with the PHP logo on its side, designed by Vincent Pontier in 1998. The elePHPant is sometimes differently colored when in plush toy form.

PHP Can Do

PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynamic page content, or send and receive cookies. But PHP can do much more.

There are three main areas where PHP scripts are used:

  • Server-Side Scripting: This is the most traditional and main target field for PHP. You need three things to make this work. The PHP parser (CGI or server module), a web server and a web browser. You need to run the web server, with a connected PHP installation. You can access the PHP program output with a web browser, viewing the PHP page through the server. All these can run on your home machine if you are just experimenting with PHP programming.
  • Command Line Scripting: You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or Task Scheduler (on Windows). These scripts can also be used for simple text processing tasks.
  • Writing Desktop Applications: PHP is probably not the very best language to create a desktop application with a graphical user interface, but if you know PHP very well, and would like to use some advanced PHP features in your client-side applications you can also use PHP-GTK to write such programs. You also have the ability to write cross-platform applications this way. PHP-GTK is an extension to PHP, not available in the main distribution.



PHP Environment Setup


In order to develop and run PHP Web pages three vital components need to be installed on your computer system.


  • Web Server: PHP will work with virtually all Web Server software, including Microsoft's Internet Information Server (IIS) but then most often used is freely available Apache Server.
  • Database: PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database.
  • PHP Parser: In order to process PHP script instructions a parser must be installed to generate HTML output that can be sent to the Web Browser. 

Or you can download WAMP Server/XAMP Server/ LAMP Server (these server packages include Data Base, Web Server & PHP).

The original, only complete and most widely used PHP implementation is powered by the Zend Engine and known simply as PHP. To disambiguate it from other implementations, it is sometimes unofficially referred to as "Zend PHP". The Zend Engine compiles PHP source code on-the-fly into an internal format that it can execute, thus it works as an interpreter.

Zend Engine

The Zend Engine is the open source scripting engine that interprets the PHP programming language. It was originally developed by Andi Gutmans and Zeev Suraski while they were students at the Technion - Israel Institute of Technology. They later founded a company called Zend Technologies in Ramat Gan, Israel. The name Zend is a combination of their forenames, Zeev and Andi.

The first version of the Zend Engine appeared in 1999 in PHP version 4. It was written in C as a highly optimized modular back-end, which for the first time could be used in applications outside of PHP. The Zend Engine provides memory and resource management, and other standard services for the PHP language. Its performance, reliability and extensibility played a significant role in PHP's increasing popularity.

PHP Syntax Overview

Commenting PHP Code


  • Single-Line Comments: They are generally used for short explanations or notes relevant to the local code. Here are the examples of single line comments.

# This is a comment, and
# This is the second line of the comment
// This is a comment too. Each style comments only

  • Multi-Lines Printing: Here are the examples to print multiple lines in a single print statement −

# First Example
print <<<END
   This uses the "here document" syntax to output
   multiple lines with $variable interpolation. Note
   that the here document terminator must appear on a
   line with just a semicolon no extra whitespace!
   END;

# Second Example
   print "This spans
   multiple lines. The newlines will be
   output as well";

  • Multi-Lines Comments: They are generally used to provide pseudocode algorithms and more detailed explanations when necessary. The multiline style of commenting is the same as in C. Here are the example of multi lines comments.

/* This is a comment with multiline
      Author : ABC
      Purpose: Multiline Comments Test
      Subject: PHP
   */

PHP is Whitespace Insensitive

Whitespace is the stuff you type that is typically invisible on the screen, including spaces, tabs, and carriage returns (end-of-line characters).

PHP whitespace insensitive means that it almost never matters how many whitespace characters you have in a row.one whitespace character is the same as many such characters.

For example, each of the following PHP statements that assigns the sum of 2 + 2 to the variable $four is equivalent:

$four = 2 + 2; // single spaces
$four <tab>=<tab>2<tab>+<tab>2 ; // spaces and tabs
$four =
2+
2; // multiple lines

PHP is Case Sensitive

PHP is a case sensitive language.

<html>
   <body> 
      <?php
         $capital = 67;
         print("Variable capital is $capital<br>");
         print("Variable CaPiTaL is $CaPiTaL<br>");
      ?>
   </body>
</html>

This will produce the following result:

Variable capital is 67
Variable CaPiTaL is

The main way to store information in the middle of a PHP program is by using a variable.

Here are the most important things to know about variables in PHP:

  • All variables in PHP are denoted with a leading dollar sign ($).
  • The value of a variable is the value of its most recent assignment.
  • Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.
  • Variables can, but do not need, to be declared before assignment.
  • Variables in PHP do not have intrinsic types - a variable does not know in advance whether it will be used to store a number or a string of characters.
  • Variables used before they are assigned have default values.
  • PHP does a good job of automatically converting types from one to another when necessary.
  • PHP variables are Perl-like.

PHP has a total of eight data types which we use to construct our variables:

  • Integers: are whole numbers, without a decimal point, like 4195.
  • Doubles: are floating-point numbers, like 3.14159 or 49.1.
  • Booleans: have only two possible values either true or false.
  • NULL: is a special type that only has one value: NULL.
  • Strings: are sequences of characters, like 'PHP supports string operations.'
  • Arrays: are named and indexed collections of other values.
  • Objects: are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
  • Resources: are special variables that hold references to resources external to PHP (such as database connections).

The first five are simple types, and the next two (arrays and objects) are compound - the compound types can package up other arbitrary values of arbitrary type, whereas the simple types cannot.

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 can handle forms, i.e. gather data from files, save data to a file, thru email you can send data, return data to the user.
  • You add, delete, modify elements within your database thru 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

The best things in using PHP are that it is extremely simple for a newcomer, but offers many advanced features for a professional programmer.

----

No comments:

Post a Comment