Skip to main content

Über dieses Buch

This pocket reference guide has been updated with the new PHP 7.0 release. It is a condensed, code-rich scripting and syntax handbook for the PHP scripting language. PHP 7 Quick Scripting Reference presents the essential PHP script in a well-organized format.

You won’t find any technical jargon, bloated samples, drawn out history lessons or witty stories in this book. What you will find is a Web scripting language reference that is concise, to the point and highly accessible. The book is packed with useful information and is a must-have for any PHP programmer or Web developer.

In it, you will find a concise reference to the PHP 7 scripting language syntax. It includes short, simple and focused code examples and a well laid out table of contents and a comprehensive index allowing easy review.

What you’ll learn

What is new in PHP 7 and how to get started with it

What are variables, operators, strings, arrays, conditionals, loops and other language constructs

How to group and reuse code with functions, methods and namespaces

How to use object-oriented features such as classes, inheritance, abstract classes and interfaces

How to import files and retrieve user dataWhat are type declarations and type conversions

How to test variables, create references and use overloading methods

How to store user data with cookies and sessions

How to deal with errors through error handling, exception handling and assertions

Who this book is for

This book is a handy, pocket quick scripting syntax reference for experienced PHP as well as perhaps other programmers and Web developers even new to PHP.<



Chapter 1. Using PHP

To start developing in PHP, create a plain text file with a .php file extension and open it in the editor of your choice—for 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.

Mikael Olsson

Chapter 2. Variables

Variables are used for storing data, such as numbers or strings, so that they can be used multiple times in a script.

Mikael Olsson

Chapter 3. Operators

An operator is a symbol that makes the script perform a specific mathematical or logical manipulation. The operators in PHP can be grouped into five types: arithmetic, assignment, comparison, logical, and bitwise operators.

Mikael Olsson

Chapter 4. String

A string is a series of characters that can be stored in a variable. In PHP, strings are often delimited by single quotes.

Mikael Olsson

Chapter 5. Arrays

An array is used to store a collection of values in a single variable. Arrays in PHP consist of key-value pairs. The key can either be an integer (numeric array), a string (associative array), or a combination of both (mixed array). The value can be any data type.

Mikael Olsson

Chapter 6. Conditionals

Conditional statements are used to execute different code blocks based on different conditions.

Mikael Olsson

Chapter 7. Loops

There are four looping structures in PHP. These are used to execute a specific code block multiple times. Just as with the conditional if statement, the curly brackets for the loops can be left out if there is only one statement in the code block.

Mikael Olsson

Chapter 8. Functions

Functions are reusable code blocks that only execute when called. They allow the code to be divided into smaller parts that are easier to understand and reuse.

Mikael Olsson

Chapter 9. Class

A Class is a template used to create objects. To define one, the class keyword is used, followed by a name and a code block. The naming convention for classes is mixed case, meaning that each word should be initially capitalized.

Mikael Olsson

Chapter 10. Inheritance

Inheritance allows a class to acquire the members of another class. In the following example, the Square class inherits from Rectangle, specified by the extends keyword. Rectangle then becomes the parent class of Square, which in turn becomes a child class of Rectangle. In addition to its own members, Square gains all accessible (non-private) members in Rectangle, including any constructor.

Mikael Olsson

Chapter 11. Access Levels

Every class member has an accessibility level that determines where the member is visible.

Mikael Olsson

Chapter 12. Static

The static keyword can be used to declare properties and methods that can be accessed without having to create an instance of the class.

Mikael Olsson

Chapter 13. Constants

A constant is a variable with a value that cannot be changed by the script.

Mikael Olsson

Chapter 14. Interface

An interface specifies methods that classes using the interface must implement.

Mikael Olsson

Chapter 15. Abstract

An abstract class provides a partial implementation that other classes can build upon. When a class is declared as abstract, it means that the class can contain incomplete methods that must be implemented in child classes, in addition to normal class members.

Mikael Olsson

Chapter 16. Traits

A trait is a group of methods that can be inserted into classes. They were added in PHP 5.4 to enable greater code reuse without the added complexity that comes from allowing multiple inheritance.

Mikael Olsson

Chapter 17. Importing Files

The same code often needs to be called on multiple pages. This can be done by first placing the code inside a separate file and then including that file using the include statement. This statement takes all the text in the specified file and includes it in the script, as if the code had been copied to that location.

Mikael Olsson

Chapter 18. Type Declarations

Type declarations allow a function to declare the expected types of their parameters and return value. This permits the PHP engine to enforce that the specified types are used.

Mikael Olsson

Chapter 19. Type Conversions

PHP automatically converts a variable’s data type as necessary, given the context in which it is used. For this reason, explicit type conversions are seldom required. Nonetheless, the type of a variable or expression may be changed by performing an explicit type cast.

Mikael Olsson

Chapter 20. Variable Testing

As a web-focused language, it is common in PHP to process user-supplied data. Such data needs to be tested before it is used to confirm that it exists and has a valid value. PHP provides a number of built-in constructs that can be used for this purpose.

Mikael Olsson

Chapter 21. Overloading

Overloading in PHP provides the ability to add object members at run-time.

Mikael Olsson

Chapter 22. Magic Methods

There are a number of methods that can be implemented in a class for the purpose of being called internally by the PHP engine.

Mikael Olsson

Chapter 23. User Input

When an HTML form is submitted to a PHP page, the data becomes available to that script.

Mikael Olsson

Chapter 24. Cookies

A cookie is a small file kept on the client’s computer that can be used to store data relating to that user.

Mikael Olsson

Chapter 25. Sessions

A session provides a way to make variables accessible across multiple web pages. Unlike cookies, session data is stored on the server.

Mikael Olsson

Chapter 26. Namespaces

Namespaces provide a way to avoid naming conflicts and to group namespace members into a hierarchy. Any code may be contained within a namespace, but only four code constructs are affected: classes, interfaces, functions, and constants.

Mikael Olsson

Chapter 27. References

A reference is an alias that allows two different variables to write to the same value. There are three operations that can be performed with references: assign by reference, pass by reference, and return by reference.

Mikael Olsson

Chapter 28. Advanced Variables

In addition to being a container for data, PHP variables have other features that are examined in this chapter. These are features that are not commonly used but are good to know about.

Mikael Olsson

Chapter 29. Error Handling

An error is a mistake in the code that the developer needs to fix. When an error occurs in PHP, the default behavior is to display the error message in the browser. This message includes the file name, line number, and error description in order to help the developer correct the problem.

Mikael Olsson

Chapter 30. Exception Handling

PHP 5 introduced exceptions, a built-in mechanism for handling program failures within the context in which they occur. Unlike errors, which generally need to be fixed by the developer, exceptions are handled by the script. They represent an irregular run-time situation that should have been expected as a possibility and which the script should be able to handle on its own.

Mikael Olsson

Chapter 31. Assertions

Assert is a debugging feature that can be used during development to ensure that a condition is always true. Any expression can be asserted, as long as it evaluates to either true or false.

Mikael Olsson


Weitere Informationen

Premium Partner