PHP (programming language)

From Bauman National Library
This page was last modified on 8 June 2016, at 20:08.
PHP (programming language)
300px
Paradigm Imperative, functional, object-oriented, procedural, reflective
Designed by Rasmus Lerdorf
Developer Zend Technologies
First appeared 1995; 27 years ago (1995)[1]
Stable release 7.0.5[2] / March 31, 2016; 6 years ago (2016-03-31)
Typing discipline Dynamic, weak
Implementation language C (primarily; some components C++)
OS Unix-like, Windows
License PHP License (most of Zend engine under Zend Engine License)
Filename extensions .php, .phtml, .php3, .php4, .php5, .phps
Website php.net
Major implementations
Zend Engine, HHVM, Phalanger, Quercus, Project Zero, Parrot
Influenced by
Perl, C, C++, Java, Tcl[1]
Influenced
Falcon, Hack

PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. PHP code may be embedded into HTML code, or it can be used in combination with various Web template systems and web frameworks[3].

Data types

PHP is a programming language with dynamic typing, do not requiring to point the type when declaring variables. There ara the following data type in PHP:

Scalar:

  • Integer
  • Float, double
  • Boolean
  • String
  • NULL

Not scalar:

  • Resource
  • Array
  • Object

Pseudotypes:

  • Mixed
  • Number
  • Callback
  • Void

Variables

All variable names must begin with $. Variable names are case sensitive[4]. Variable names must not begin with digit, but they can begin with '_'. There are some examples:

<?php
    $var = "Bob";
    $Var = "Joe";
    $_4site = 'not yet';    
?>

Comments

There are three comment types in PHP programming language:

<?php
     echo "Это тест"; // Single - line comment
     /* Multiline 
        comment */   
     echo "Last example"; # This is a comment in a Unix shell style
?>

Constants

In PHP constants are defined with the help of define() function , which has the following format[4]:

define ($name, $value, $case_sen), where:
$name - constant name;
$value - constant value;
$case_sen - optional parameter, if true - constant name will be case sensitive.

Constant example:

<?php
    define("pi",3.14,true);
?>

Constants must be used without $.

Standard constants

PHP provides a large list of predefined constants for each executable script. Many of these constants are defined by different units will be present only when these modules are available as a result of dynamic loading or as a result of the static assembly[4]. Special constants are case-insensitive. There is the list of such constants:

Constant name Description
__LINE__ Current line in file.
__FILE__ The full path and name of the current file.
__DIR__ The full path to the file without its name.
__FUNCTION__ Function name. (Was added in PHP 4.3.0.)
__CLASS__ Class name. (Was added in PHP 4.3.0.)
__METHOD__ Class method name. (Was added in PHP 5.0.0)
__NAMESPACE__ Current namespace name.

Operators

PHP supports following operators:

Arithmetic operators

Operator example Operator name
-$a Negation
$a + $b Addition
$a - $b Subtraction
$a * $b Multiplication
$a / $b Division
$a % $b Modulo
$a ** $b Exponentiation

Increment and decrement operators

Operator example Operator name
++$a Prefix increment
$a++ Postfix increment
--$a Prefix decrement
$a-- Postfix decrement

Bitwise operators

Operator example Operator name
$a & $b Bit 'and'
$b Bit 'or'
$a ^ $b Bit 'xor'
~ $a Negation
$a << $b Left shift
$a >> $b Right shift

Comparison operators

Operator example Operator name
$a == $b Equal
$a === $b Identically equal
$a != $b Not equal
$a <> $b Not equal
$a ! == $b Identically not equal
$a < $b Less
$a > $b More
$a <= $b Less or equal
$a >= $b More or equal

Logical operators

Operator example Operator name
$a and $b Logical and'
$a or $b Logical 'or'
$a xor $b Logical 'xor'
 ! $a Negation

User defined functions

A custom function can be declared in any part of the program (script) to the place of its first use[4]. The syntax for declaring functions is follows:

function Name (arg1[=value1],...,argN[=valueN])
{
    function body
}

The custom function example:

<?php 
    function makecoffee($type)
    {
        return "A cup of $type.\n";
    }
?>

Fundamentals of OOP in PHP

OOP in PHP, like in many other programming languages, based on the encapsulation, polymorphism and inheritance. Description of classes in PHP begins with official word class:

class Class_Name {
    //  class members description - data and methods for processing
}

To create an instance of the class you should use operator 'new':

Object = new Class_Name;

Class example is below:

<?php
    class Coor {
        var $name;
        var $addr;

        function Name() {
           echo "$name";
        }
    }
?>

To access the class members in PHP you shoul use operator '->':

<?php
    $object = new Coor;
    $object->name = "Alex";
    echo $object->name;
    $object->Getname();
?>

References

  1. 1.0 1.1 Lerdorf, Rasmus (2007-04-26). "PHP on Hormones&nbsp;– history of PHP presentation by Rasmus Lerdorf given at the MySQL Conference in Santa Clara, California". The Conversations Network. Retrieved 2009-12-11. 
  2. "News Archive[[:Template:Snd]] 2016: PHP 7.0.5 Released". php.net. 2016-03-31. Retrieved 2016-03-31.  URL–wikilink conflict (help)
  3. https://en.wikipedia.org/wiki/PHP
  4. 4.0 4.1 4.2 4.3 http://www.php.su