Technews 4 everyone: PHP


PHP: Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document. As a general-purpose programming language, PHP code is processed by an interpreter application in command-line mode performing desired operating system operations and producing program output on its standard output channel. It may also function as a graphical application. PHP is available as a processor for most modern web servers and as a standalone interpreter on most operating systems and computing platforms.
PHP was originally created by Rasmus Lerdorf in 1995[1][2] and has been in continuous development ever since. The main implementation of PHP is now produced by the PHP Group and serves as the de facto standard for PHP as there is no formal specification.[3] PHP is free software released under the PHP License.


    PHP Test
  echo 'Hello World';
  /* echo("Hello World"); works as well, although echo isn't a
  function, but a language construct. In some cases, such
  as when multiple parameters are passed to echo, parameters
  cannot be enclosed in parentheses. */

PHP code embedded within HTML code
PHP only parses code within its delimiters. Anything outside its delimiters is sent directly to the output and is not processed by PHP (although non-PHP text is still subject to control structures described within PHP code). The most common delimiters are to open and ?> to close PHP sections. delimiters are also available, as are the shortened forms or (which is used to echo back a string or variable) and ?> as well as ASP-style short forms <% or <%= and %>. While short delimiters are used, they make script files less portable as support for them can be disabled in the PHP configuration, and so they are discouraged.[53] The purpose of all these delimiters is to separate PHP code from non-PHP code, including HTML.[54]
The first form of delimiters, and ?>, in XHTML and other XML documents, creates correctly formed XML 'processing instructions'.[55] This means that the resulting mixture of PHP code and other markup in the server-side file is itself well-formed XML.
Variables are prefixed with a dollar symbol and a type does not need to be specified in advance. Unlike function and class names, variable names are case sensitive. Both double-quoted ("") and heredoc strings allow the ability to embed a variable's value into the string.[56] PHP treats newlines as whitespace in the manner of a free-form language (except when inside string quotes), and statements are terminated by a semicolon.[57] PHP has three types of comment syntax: /* */ marks block and inline comments; // as well as # are used for one-line comments.[58] The echo statement is one of several facilities PHP provides to output text (e.g. to a web browser).
In terms of keywords and language syntax, PHP is similar to most high level languages that follow the C style syntax. if conditions, for and while loops, and function returns are similar in syntax to languages such as C, C++, Java and P

 Data types
PHP stores whole numbers in a platform-dependent range, either a 64-bit or 32-bit signed integer equivalent to the C-language long type. Unsigned integers are converted to signed values in certain situations; this behavior is different from other programming languages.[59] Integer variables can be assigned using decimal (positive and negative), octal, and hexadecimal notations. Floating point numbers are also stored in a platform-specific range. They can be specified using floating point notation, or two forms of scientific notation.[60] PHP has a native Boolean type that is similar to the native Boolean types in Java and C++. Using the Boolean type conversion rules, non-zero values are interpreted as true and zero as false, as in Perl and C++.[60] The null data type represents a variable that has no value. The only value in the null data type is NULL.[60] Variables of the "resource" type represent references to resources from external sources. These are typically created by functions from a particular extension, and can only be processed by functions from the same extension; examples include file, image, and database resources.[60] Arrays can contain elements of any type that PHP can handle, including resources, objects, and even other arrays. Order is preserved in lists of values and in hashes with both keys and values, and the two can be intermingled.[60] PHP also supports strings, which can be used with single quotes, double quotes, or heredoc syntax.[61]
The Standard PHP Library (SPL) attempts to solve standard problems and implements efficient data access interfaces and classes.[62]


PHP has hundreds of base functions and thousands more via extensions. These functions are well documented on the PHP site; however, the built-in library has a wide variety of naming conventions and inconsistencies.[citation needed] PHP currently has no functions for thread programming, although it does support multiprocess programming on POSIX systems.[63]
You can create your own functions, like this:

    function myFunction(){
        return 'Tyler';
    echo 'My name is ' . myFunction() . '!';

PHP 5.2 and earlier

Functions are not first-class functions and can only be referenced by their name, directly or dynamically by a variable containing the name of the function.[64] User-defined functions can be created at any time without being prototyped.[64] Functions can be defined inside code blocks, permitting a run-time decision as to whether or not a function should be defined. Function calls must use parentheses, with the exception of zero argument class constructor functions called with the PHP new operator, where parentheses are optional. PHP supports quasi-anonymous functions through the create_function() function, although they are not true anonymous functions because anonymous functions are nameless, but functions can only be referenced by name, or indirectly through a variable $function_name();, in PHP.[64]

 PHP 5.3 and newer

PHP gained support for closures. True anonymous functions are supported using the following syntax:
function getAdder($x) {
    return function ($y) use ($x) {
        return $x + $y;
$adder = getAdder(8);
echo $adder(2); // prints "10"
Here, the getAdder() function creates a closure using the parameter $x (the keyword "use" imports a variable from the lexical context), which takes an additional argument $y and returns it to the caller. Such a function is a first class object, that means, it can be stored, passed as a parameter to other functions, etc. For more details see Lambda functions and closures RFC.
The goto flow control statement is used as follows:
function lock() {
    $file = fopen('file.txt', 'r+');
    if (!flock($file, LOCK_EX)) {
        goto retry;
    fwrite($file, 'Success!');
    return 0;
When lock() is called, PHP opens a file and tries to lock it. retry:, the target label, defines the point to which execution should return if flock() is unsuccessful and the goto retry; is called. goto is restricted and requires that the target label be in the same file and context.
goto is supported by PHP versions 5.3+


Basic object-oriented programming functionality was added in PHP 3 and improved in PHP 4.[3] Object handling was completely rewritten for PHP 5, expanding the feature set and enhancing performance.[65] In previous versions of PHP, objects were handled like value types.[65] The drawback of this method was that the whole object was copied when a variable was assigned or passed as a parameter to a method. In the new approach, objects are referenced by handle, and not by value. PHP 5 introduced private and protected member variables and methods, along with abstract classes and final classes as well as abstract methods and final methods. It also introduced a standard way of declaring constructors and destructors, similar to that of other object-oriented languages such as C++, and a standard exception handling model. Furthermore, PHP 5 added interfaces and allowed for multiple interfaces to be implemented. There are special interfaces that allow objects to interact with the runtime system. Objects implementing ArrayAccess can be used with array syntax and objects implementing Iterator or IteratorAggregate can be used with the foreach language construct. There is no virtual table feature in the engine, so static variables are bound with a name instead of a reference at compile time.[66]
If the developer creates a copy of an object using the reserved word clone, the Zend engine will check if a __clone() method has been defined or not. If not, it will call a default __clone() which will copy the object's properties. If a __clone() method is defined, then it will be responsible for setting the necessary properties in the created object. For convenience, the engine will supply a function that imports the properties of the source object, so that the programmer can start with a by-value replica of the source object and only override properties that need to be changed.[67]
Basic example of object-oriented programming as described above:
class Person {
   public $first;
   public $last;
   public function __construct($f, $l = '') { //Optional parameter
       $this->first = $f;
       $this->last = $l;
   public function greeting() {
       return "Hello, my name is " . $this->first . " " . $this->last . ".";
   static public function staticGreeting($first, $last) {
       return "Hello, my name is " . $first . " " . $last . ".";
$him = new Person('John', 'Smith');
$her = new Person('Sally', 'Davis');
$other= new Person('Joe');
echo $him->greeting(); // prints "Hello, my name is John Smith."
echo '
echo $her->greeting(); // prints "Hello, my name is Sally Davis."
echo '
echo $other->greeting(); // prints "Hello, my name is Joe  ."
echo '
echo Person::staticGreeting('Jane', 'Doe'); // prints "Hello, my name is Jane Doe."
Source Info:


Post a Comment


Ingresa tu correo electronico y recibe lo mejor de Technews4everyone en tu inbox:

Delivered by FeedBurner