PHPلغة PHP
مقدمة الفصل
ماذا ستتعلم في هذا الفصل
- Manipulate data of various types.
- Use operators, arrays and control statements.
- Use regular expressions to search for text that matches a pattern.
- Construct programs that process form data.
- Store data on the client using cookies.
- Create programs that interact with MySQL databases.
أقسام الفصل
- 1What PHP is and a simple PHP programما PHP وأول برنامج
- 2Data types and converting between themأنواع البيانات والتحويل بينها
- 3Constants, uninitialized variables, operators and keywordsالثوابت والمتغيرات غير المهيأة والعمليات
- 4Arraysالمصفوفات
- 5String comparisonsمقارنة النصوص
- 6Regular expressionsالتعابير النمطية
- 7Form processing and business logicمعالجة النماذج ومنطق العمل
- 8Reading from a databaseالقراءة من قاعدة البيانات
- 9Cookiesملفات تعريف الارتباط
- 10Dynamic contentالمحتوى الديناميكي
تلميح: استخدم أسهم لوحة المفاتيح للتنقل بين الشرائح.
What is PHP?ما PHP؟
What PHP is and a simple PHP program
The most popular server-side scripting language for creating dynamic web pages. PHP is open source and platform independent, and supports a large number of databases.
- PHP code is embedded in text-based documents such as HTML, but is interpreted by a server before being delivered to the client.
- PHP script file names end with .php.
- Code is inserted between the scripting delimiters <?php and ?>.
Variables, statements and commentsالمتغيرات والجمل والتعليقات
What PHP is and a simple PHP program
- Variables are preceded by a $ and are created the first time they're encountered. Names are case sensitive.
- Statements terminate with a semicolon (;) — forgetting it is a syntax error.
- Comments: // or # for one line; /* … */ for several lines.
- A variable inside a double-quoted string is interpolated (its value is inserted) — on the server, before the page is sent.
- PHP variables are loosely typed: they can hold different types of data at different times.
سؤال سريع
What PHP is and a simple PHP program
PHP data typesأنواع البيانات
Data types and converting between them
| Typeالنوع | Description (Fig. 19.2)الوصف |
|---|---|
| int, integer | Whole numbers (numbers without a decimal point) |
| float, double, real | Real numbers (numbers containing a decimal point) |
| string | Text enclosed in single ('') or double ("") quotes |
| bool, boolean | true or false |
| array | Group of elements |
| object | Group of associated data and methods |
| resource | An external source — usually information from a database |
| NULL | No value |
Converting between typesالتحويل بين الأنواع
Data types and converting between them
Returns the current type of its argument.
Takes a variable and its new type. Can result in loss of data: doubles are truncated when converted to integers.
Uses the number at the beginning of the string; if there is none, the string evaluates to 0.
e.g. (integer) $data — does not change the variable's content; it creates a temporary copy of its value.
Combines multiple strings.
سؤال سريع
Data types and converting between them
Constants and uninitialized variablesالثوابت والمتغيرات غير المهيأة
Constants, uninitialized variables, operators and keywords
Creates a named constant: the name and the value; an optional third boolean makes it case insensitive (constants are case sensitive by default). Assigning a value to a constant after it is declared is a syntax error.
- An uninitialized variable in a numeric context evaluates to 0 — e.g., multiplying by it gives 0. Initialize variables before using them.
- Keywords may not be used as function, method, class or namespace names.
Operator precedenceأسبقية العمليات
Constants, uninitialized variables, operators and keywords
From Fig. 19.6 (highest first, selected rows):
- ++ -- (increment, decrement)
- ! (not)
- \* / % (multiplication, division, modulus) — left to right
- + - . (addition, subtraction, concatenation) — left to right
- < > <= >= (relational)
- == != === !== (equality, identity)
- && then || (logical AND, OR) — left to right
- ?: (ternary conditional)
- = += -= \*= /= %= .= (assignment) — right to left
- and, xor, or — left to right
Arrays and associative arraysالمصفوفات
- Array names begin with $; elements are accessed with an index in [ ].
- Assigning to an element of a nonexistent array creates it; omitting the index ($a[] = …) appends to the end.
- count returns the number of elements; array creates an array starting at index 0.
An array with nonnumeric indices, created with => (index on the left, value on the right).
Sets the internal pointer to the first element.
Returns the index of the element currently referenced.
Moves the internal pointer to the next element.
سؤال سريع
Arrays
foreachالحلقة foreach
Arrays
Designed for iterating through arrays: the array, the keyword as, then two variables — the first gets the index, the second the value. With only one variable after as, it gets the value.
سؤال سريع
Arrays
String comparisonsمقارنة النصوص
- String tasks can use the equality and relational operators (==, !=, <, <=, >, >=).
Compares two strings and returns -1 if the first alphabetically precedes the second, 0 if they are equal, 1 if the first follows the second.
سؤال سريع
String comparisons
Regular expressionsالتعابير النمطية
A series of characters that serve as pattern-matching templates (search criteria) in strings, text files and databases.
Searches a string for a pattern (PCRE). The optional third argument stores the matches: element 0 = whole match, then each parenthetical part.
Beginning of a string; end of a string; any single character.
Match any single character from the list; ranges use a dash, e.g. [a-zA-Z].
Beginning / end of a word — matches whole words.
How often a character or set of characters can appear in a match.
Character classes are enclosed by [: and :]; inside another set of brackets they match all characters in the class.
Returns the modified string; used to remove matches before searching again.
سؤال سريع
Regular expressions
Form processingمعالجة النماذج
Associative arrays predefined by PHP holding variables from user input, the environment or the web server, accessible in any variable scope. $_GET and $_POST hold data sent by HTTP get and post requests.
Escapes the normal meaning of a character in a string.
Terminates script execution; its optional string argument is printed as the script exits.
سؤال سريع
Form processing and business logic
Reading from a MySQL databaseقواعد البيانات
Reading from a database
| Functionالدالة | What it does (slides 52–53)وظيفتها |
|---|---|
| mysql_connect(host, user, password) |
|
| mysql_select_db |
|
| mysql_query(query, database) |
|
| mysql_error | Returns any error strings from the database |
| mysql_close | Closes the connection given as its argument |
| mysql_fetch_row | Returns an array of the column values in the current row |
- Fig. 19.16 prints the number of results with mysql_num_rows( $result ).
سؤال سريع
Reading from a database
Cookiesملفات تعريف الارتباط
A piece of information stored by a server in a text file on a client's computer to maintain information about the client during and between browsing sessions. A server can access only the cookies it has placed.
No expiration → session cookie (until the browser closes); with expiration → persistent cookie. Only the name → the cookie is deleted. Must be called before any other output (sent with the HTTP header).
Superglobal array with all cookie values indexed by their names.
سؤال سريع
Cookies
Dynamic contentالمحتوى الديناميكي
Determines whether the $_POST array contains keys for the form fields.
A variable variable: the value of the variable whose name equals the value of $variable.
Inserts a backslash (\) before any special characters in the passed string.
الخلاصة
أنهيت الدرس
الخطوة التالية: ثبّت المعلومات بالبطاقات، ثم اختبر نفسك بالأسئلة.
ما راجعته في هذا الدرس
- What PHP is and a simple PHP programما PHP وأول برنامج
- Data types and converting between themأنواع البيانات والتحويل بينها
- Constants, uninitialized variables, operators and keywordsالثوابت والمتغيرات غير المهيأة والعمليات
- Arraysالمصفوفات
- String comparisonsمقارنة النصوص
- Regular expressionsالتعابير النمطية
- Form processing and business logicمعالجة النماذج ومنطق العمل
- Reading from a databaseالقراءة من قاعدة البيانات
- Cookiesملفات تعريف الارتباط
- Dynamic contentالمحتوى الديناميكي