The following ‘Hi, what’s up?’ program is in black and white
in PHP code set in an HTML document: <!DOCTYPE
html><html> <head><title>PHP</title></head>
<body> <?php echo “<p> Hi, what’s up?</p>”; ?> </body></html> However, as no condition exists for
PHP code to be implanted in HTML, the simplest version of Hello, World! may be printed
like this, with the closing tag absent as favorite in files
containing pure PHP
code <?='Hello world'; The PHP prophet only executes PHP code within its delimiters.
Anything outside its delimiters is not processed by PHP, though non-PHP text is
still subject to manage structures described in PHP code. The most common
delimiters are <?php to open and ?> to close PHP sections. The
shortened form <? Also exists. This short delimiter makes script
files less portable, since bear for them can be disabled in the local PHP intend
and it is therefore downcast. However, there is no proposal against the use of
the echo short tag <?=.Prior to PHP 5.4.0, this short syntax for
echo() only works with the short_open_tag pattern set enabled, while for PHP
5.4.0 and later it is always obtainable. The idea of all these delimiters is to
separate PHP code from non-PHP content, for example JavaScript code or HTML
markup. The first form of delimiters, <?php and ?>, in XHTML and other XML
documents, creates properly formed XML processing directives. This means that
the ensuing mixture of PHP code and other markup in the server-side file is
itself well-formed XML. Variables are prefixed with a dollar figure, and a type
does not need to be particular in advance. PHP 5 introduced type hinting
that allows functions to force their parameters to be objects of a explicit
class, arrays, interfaces or callback functions. However, before PHP 7.0, type
hints could not be used with scalar types such as integer or string. Unlike
function and class names, variable names are case sensitive. Both double-quoted
("") and heredoc strings afford the ability to interpolate a
variable's value into the string. PHP treats newlines as whitespace in the approach
of a free-form language, and statements are finished by a semicolon. PHP has
three types of comment syntax: /*
*/ script block and inline comments; // as well as # are used for one-line
comments. The echo account is one of numerous facilities PHP provides to output
text, e.g., to a web browser. In terms of keywords and words syntax, PHP
is similar to the C style syntax. if conditions, for and while loops, and
function returns are alike in syntax to languages such as C, C++, C#, Java and
Perl. The following is an example of PHP for loop:<?php
for ($x = 0; $x
<= 100; $x++) {echo "The number is: $x <br>";} ?>
Data types PHP stores integers in a
platform-dependent variety, either a 64-bit or 32-bit signed integer equal to
the C-language long type. Unsigned integers are changed to signed values in
certain situations; this manner is diverse from that of other encoding
languages. Integer variables can be assigned using decimal (positive and
negative), octal, hexadecimal, and double 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. 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++. The null data type represents a variable that has no value;
NULL is
the only allowed value for this data type. Variables of the "resource"
type signify references to resources from outside sources. These are typically fashioned
by functions from a meticulous addition, and can only be processed by functions
from the same extension; examples include file, image, and database wealth. Arrays
can contain elements of any type that PHP can handle, including income,
objects, and other arrays. Order is conserved in lists of values and in hashes
with both keys and values, and the two can be intermingled. PHP also chains strings,
which can be used with single quotes, double quotes, nowdoc or heredoc syntax. The
Standard PHP Library (SPL) attempts to solve standard problems and implements competent
data access interfaces and classes.
Functions
PHP defines a great array of functions in the core language
and many also exist in various extensions; these functions are well documented
in the online PHP documentation. However, the built-in library has a wide
variety of naming conventions and allied inconsistencies, as described under history
above. Custom functions may be defined by the developer, e.g.:
function myAge($birthYear) { // defines a function, this one is named "myAge"
$yearsOld
= date('Y') - $birthYear; //
calculates the age
return
$yearsOld . ' year'
. ($yearsOld != 1 ? 's' : ''); // returns the age in a descriptive form
}
echo 'I am currently '
. myAge(1981) . ' old.'; // outputs the
text concatenated
// with the return value of myAge()
// As the result of this syntax, myAge() is
called. In 2017, the output of the above sample program is 'I am
currently 36 years old.' In lieu of function pointers, functions in PHP can be
referenced by a string containing their name. In this manner, normal PHP
functions can be used, for example, as callbacks or within function tables.
User-defined functions may be formed at any time without being prototyped.
Functions may be distinct inside code blocks, permitting a run-time decision as
to whether or not a function should be defined. There is a function exists
function that determines whether a function with a given name has previously
been defined. Function calls must use parentheses, with the exception of
zero-argument class constructor functions called with the PHP operator new, in
which case parentheses are optional. Until PHP 5.3, support for anonymous
functions and closures did not exist in PHP. While create_function() exists
since PHP 4.0.1, it is merely a thin wrapper around
eval() that allows ordinary
PHP functions to be created throughout program execution. PHP 5.3 added syntax
to define an anonymous function or "closure" which can capture
variables from the surrounding scope:
function getAdder($x) {
return
function($y) use ($x) {
return
$x + $y;
};
}
$adder = getAdder(8);
echo $adder(2); //
prints "10"
In the example above, getAdder() function creates a closure
using conceded argument $x (the keyword
functionality was added in
PHP 3 and improved in PHP 4. This allowed for PHP to gain further abstraction,
making creative tasks easier for programmers using the language. Object
handling was completely rewritten for PHP 5, expanding the feature set and
enhancing performance. In previous versions of PHP, objects were handled like value
types. The drawback of this method was that code had to make heavy use of PHP's
"reference" variables if it wanted to modify an object it was passed
rather than creating a copy of it. 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, final classes, 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.
use imports a variable from the lexical context),
which takes an additional argument $y, and returns the created closure to the
caller. Such a function is a first-class object, meaning that it can be stored
in a variable, passed as a parameter to other functions, etc. Unusually for a dynamically typed language,
PHP supports type declarations on function parameters, which are enforced at
runtime. This has been supported for classes and interfaces since PHP 5.0,
for arrays since PHP 5.1, for "callables" since PHP 5.4, and
scalar (integer, float, string and boolean) types since PHP 7.0.
PHP 7.0 also has type declarations for function return types, expressed by
placing the type name after the list of parameters, preceded by a colon. For
example, the getAdder function from the earlier example could be annotated with
types like so in PHP 7: function getAdder(int $x): \Closure { return function(int $y) use ($x) : int { return $x + $y; };
}$adder = getAdder(8); echo $adder(2); // prints "10"
echo $adder(null);
// throws an exception because an incorrect type was
passed $adder = getAdder([]); // would also
throw an exception By default, scalar type declarations follow weak
typing principles. So, for example, if a parameter's type is int, PHP would allow not
only integers, but also convertible numeric strings, floats or booleans to be
passed to that function, and would convert them. However, PHP 7 has a
"strict typing" mode which, when used, disallows such conversions for
function calls and returns within a file. Object-oriented
programming Basic object-oriented programming

