To start developing in PHP, create a plain text file with a .php file extension and open it in the editor of your choicefor example Notepad, jEdit, Dreamweaver, NetBeans, or PHPEclipse. This PHP file can include any HTML, as well as PHP scripting code. Begin by first entering the following minimal markup for an HTML 5 web document.
Embedding PHP
PHP code can be embedded anywhere in a web document in several different ways. The standard notation is to delimit the code by
Within a PHP block, the engine is said to be in PHP mode ; outside of the block, the engine is in HTML mode . In PHP mode, everything is parsed (executed) by the PHP engine; whereas in HTML mode, everything is sent to the generated web page without any execution.
The second notation for switching to PHP mode is a short version of the first where the php part is left out. Although this notation is shorter, the longer one is preferable if the PHP code needs to be portable. This is because support for the short delimiter can be disabled in the php.ini configuration file.
... ?>
A third (now obsolete) alternative was to embed the PHP code within an HTML script element with the language attribute set to php . This alternative delimiter was seldom used; support for it was removed in PHP 7.
...
Another obsolete notation that you may encounter in legacy code is when the script is embedded between ASP tags. This notation is disabled by default, but it can be enabled from the PHP configuration file. Use of this notation has long been discouraged. The ability to enable it was finally removed in PHP 7.
The last closing tag in a script file may be omitted to make the file end while it is still in PHP mode.
Installing a Web Server
To view PHP code in a browser, the code first has to be parsed on a web server with the PHP module installed. An easy way to set up a PHP environment is to download and install a distribution of the popular Apache web server called XAMPP, which comes preinstalled with PHP, Perl, and MySQL. It allows you to experiment with PHP on your own computer.
After installing the web server, point your browser to http://localhost to make sure that the server is online. It should display the index.php file, which by default is located under C:\xampp\htdocs\index.php on a Windows machine. htdocs is the folder that the Apache web server looks to for files to serve on your domain.
Hello World
Continuing from before, the simple Hello World PHP web document should look like this:
To view this PHP file parsed into HTML, save it to the web servers htdocs folder (the servers root directory) with a name such as mypage.php . Then point your browser to its path, which is http://localhost/mypage.php for a local web server.
When a request is made for the PHP web page, the script is parsed on the server and sent to the browser as only HTML. If the source code for the web site is viewed, it will not show any of the server-side code that generated the pageonly the HTML output.
Compile and Parse
PHP is an interpreted language, not a compiled language. Every time a visitor arrives at a PHP web site, the PHP engine compiles the code and parses it into HTML, which is then sent to the visitor. The main advantage of this is that the code can be changed easily without having to recompile and redeploy the web site. The main disadvantage is that compiling the code at run-time requires more server resources.
For a small web site, a lack of server resources is seldom an issue. The time it takes to compile the PHP script is also miniscule compared to other factors, such as the time required to execute database queries. However, for a large web application with lots of traffic, the server load from compiling PHP files is likely to be significant. For such a site, the script compilation overhead can be removed by precompiling the PHP code. This can be done with eAccelerator, for example, which caches PHP scripts in their compiled state.
A web site that only serves static content (the same to all visitors) has another possibility, which is to cache the fully generated HTML pages. This provides all the maintenance benefits of having a dynamic site, with the speed of a static site. One such caching tool is the W3 Total Cache plugin for the WordPress CMS.
Comments
Comments are used to insert notes into the code. They have no effect on the parsing of the script. PHP has the two standard C++ notations for single-line ( // ) and multiline ( /* */ ) comments. The Perl comment notation ( # ) may also be used to make single-line comments.
Mikael Olsson 2016
Mikael Olsson PHP 7 Quick Scripting Reference 10.1007/978-1-4842-1922-5_2
2. Variables
Variables are used for storing data, such as numbers or strings, so that they can be used multiple times in a script.
Defining Variables
A variable starts with a dollar sign ( $ ) followed by an identifier , which is the name of the variable. A common naming convention for variables is to have each word initially capitalized, except for the first one.
$myVar;
A value can be assigned to a variable by using the equals sign, or assignment operator ( = ). The variable then becomes defined or initialized .
$myVar = 10;
Once a variable has been defined, it can be used by referencing the variables name. For example, the value of the variable can be printed to the web page by using echo followed by the variables name.
echo $myVar; // "10"
Keep in mind that variable names are case sensitive. Names in PHP can include underscore characters and numbers, but they cannot start with a number. They also cannot contain spaces or special characters, and they must not be a reserved keyword.
Data Types
PHP is a loosely typed language. This means that the type of data that a variable can store is not specified. Instead, a variables data type changes automatically to hold the value that it is assigned.
$myVar = 1; // int type
$myVar = 1.5; // float type
Furthermore, the value of a variable is evaluated differently, depending on the context in which it is used.
// Float type evaluated as string type
echo $myVar; // "1.5"
Because of these implicit type conversions, knowing the underlying type of a variable is not always necessary. Nevertheless, it is important to have an understanding of the data types that PHP works with in the background. These nine types are listed in Table .
Table 2-1.
PHP Data Types
Data Type | Category | Description |
---|
int | Scalar | Integer |
float | Scalar | Floating-point number |
bool | Scalar | Boolean value |
string | Scalar | Series of characters |