PHP基础文法
快速入门
Perl的和Ruby的语法组织得非常好,于是PHP也跟着尝试将这两种语言的优势引入。
Grammar list of Perl and Ruby was so very well organized, I tried to do any PHP Taking advantage of.
The Perl with Perl Beginner to Beginner to sample code - Perl basic grammar fastest master
Route 477 - Ruby basic grammar fastest master
I think people who know something about other languages can understand somehow the PHP syntax If you are reading this.
Please tell us mistake, if any shortage etc. m (__) m
Version
PHP5.3 system has been released, but is intended for PHP5.2 system here.
1. Basis
代码块Code block
PHP code begins with the start tag of " Php". End tag is "?>". Use the end tag when you fill the PHP code in HTML, omitting the end tag has become a convention when writing the only PHP code as a library.
It had positive commentary on Kiske's: id reason for omitting the end tag. Thank you.
PHP 基本语法最快入门补充 - Absolute Playing!
打印语句print statement
I use the print / echo.
I often use var_dump () to debug. contents of the variable in var_dump () is output.
array (3) {
[0] =>
int (1)
[1] =>
int (2)
[2] =>
int (3)
}
注释Comment
单行注释(Line Comments)
多行注释 Multi-line comments
变量声明Variable declarations
It is a declaration of the variable.
脚本的执行(Execution of the script)
I run a PHP file on the command line.
$ php hoge.php
You can also write directly to the PHP code. Php?> Is not required.
$ php-r "var_dump ('a');"
Use the redirect to write the output to a file results.
$ php hoge.php> out
词法检查(Grammar check of the script,lex)
I can check grammar in php command.
$php -l hoge.php
2. 数值Numerical
Representation of the number
There integer, floating-point to a number.
4种算术操作 Four arithmetic operations
Quotient and remainder. Remove the integer part in the intval function after performing the division of normal order to obtain the quotient.
Increment and decrement
3. 字符串 String
String representation
I must be enclosed in either single or double quotes is a string. You can use special characters, and \ n (tab), such as (newline) \ t is in double quotes. In addition, it can be variable expansion within a character string enclosed in double quotes.
字符串操作String operation
4. 数组与关联数组Array, associative array
There are only associative array with PHP. Array is represented as an associative array of numbers is key. I also have the order.
1, 'b' => 2, 'c' = > 3); //associative array (1, 'a' => 1, 2)
$array3 = array; //mixed also OK
?>
Assignment of a reference element
The number of elements
Operation of the array
关联数组函数The function of associative array
6. 控制语句Control statement
if statement
Notation such as the following will also be used when you described in HTML.
hoge
if ~ else statement
Notation such as the following will also be used when you described in HTML.
hoge
foo
if ~ else if statement
elseif or else if is possible.
Notation such as the following will also be used when you described in HTML.
hoge
foo
while statement
Notation such as the following will also be used when you described in HTML.
for statement
Notation such as the following will also be used when you described in HTML.
foreach statement
I can handle each element of the associative array.
$v) {
// $k is the key of the element, $v is the value of the element
}
?>
Notation such as the following will also be used when you described in HTML.
7. 子进程Subroutine (function)
There is a function with PHP. You can use the return to return the return value.
//You can also return multiple values in an array
function Get_multi ($v1, $v2) {
$v1 + = 100;
$v2 + = 200;
Return array ($v1, $v2);
}
list ($ret1, $ret2) = Get_multi(1, 2); //$ret1 = 101, $ret2 = 202
?>
8. 文件的输入输出Input and output files
There are several ways to file input and output.
fopen function
Make the input and output files using the file pointer.
file function
It is stored in the array reads the entire file.
file_get_contents function / file_put_contents function
and then stored as a string by reading the entire file file_get_contents function. file_put_contents function writes to the file all the value of a variable.
You should know is good grammar
Truth-value
In the following cases: in PHP, they are determined to be false.
boolean FALSE
0 (zero) of integer
0.0 (zero) of float
"0" of string and an empty string,
Array number of elements is zero
Number of objects in the member variable is zero (only PHP 4)
(Including a variable whose value has not been set) special value NULL
SimpleXML objects created from empty tags
== And ===
== /! = In comparison operators such as, automatic conversion of numeric, string is performed. It may lead to results thus not intended.
In these cases, it is possible to compare exactly also the type of the variable === /! With ==.
Whether a variable is defined
You can use the isset function to find out whether a variable is defined. True is returned if it is defined. However, the value of the variable is returned is false can be NULL in the isset function.
命令行参数 Command line arguments
You can use the $argv variable to receive the command-line arguments.
array_map
You can use the array_map function, you can receive as a new associative array with the process to each element of the associative array.
array_filter
You can use the array_filter function, you can receive as a new associative array only the elements that match the conditions.
2);');
?>
多变量赋值Assignment to multiple variables
php.ini
There is a configuration file with PHP. It is important to note behavior will change depending on the setting. In addition to the configuration file called php.ini, this setting can be set in the source code httpd.conf, and. Htaccess,.
The setting method will vary depending on the item, but you can use the () ini_set is often when setting in the source code.
The current settings, I can check the php phpinfo command or function.
# Output all the setting value
$php-i
#grepin
$php-i|grepinclude_path
Class definition
I can define a class in the class.
name = $name;
}
public function hello () {
printf ("!% s: Hello \n", $this-> name);
}
}
$User = new User ('Mike');
$user-> hello ();
?>
I can also be inherited. You can only single inheritance.
Exception
You can throw an exception throw. I will catch an exception in a try / catch. There is no equivalent to the finally in the other languages.
GetTraceAsString ();
}
?>
PHP参考资料
Official Manual
For PHP books have been published many, but most would be helpful after all is the official manual.
PHP: PHP Manual - Manual
The TIps you a little when using the official manual.
When you open the official manual in a browser, the page opens directly when you enter the name of the function was examined behind http://php.net/. You can also keyword similar If no match is displayed most, to choose from among the candidates.
http://php.net/array
Coding conventions
I have a school several coding conventions, but the coding conventions of Zend Framework will help you.
Zend Framework PHP Coding Standard - Zend Framework Manual
Modern PHP
There are also function as an object-oriented language with PHP. The following publications will be helpful.
Sweet @ sotarok with rice and meat - and the documentation that you held a study session modern PHP
Framework
Be used framework is becoming a major in Web systems development using PHP.
There is an open source framework for many, the major ones are as follows.
CakePHP
symfony
Zend Framework
Ethna
CodeIgniter
Related entries
I write the SQL for any CakePHP
Problem of seven that occur when migrating to PHP5 the script PHP4
Indirect modification of overloaded property occurs in a foreach
I use the () is_null whether PHP NULL
2 Using Mock objects with PHPUnit