Grammar list of Perl and Ruby was so very well organized, I tried to do any PHP Taking advantage of.
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
PHP5.3 system has been released, but is intended for PHP5.2 system here.
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.
Supplement of PHP basic grammar fastest master - Absolute Playing!
[PHP] $ I = 1; hoge ($ I); ?> [/ PHP] [PHP] hoge (); [/ PHP]
I use the print / echo.
[PHP]
"! Hello World" Print;
"! Hello World" echo;
[/PHP]
I often use var_dump () to debug. contents of the variable in var_dump () is output.
[PHP]
$ array = array (1,2,3);
var_dump ($ array);
array (3) {
[0] =>
int (1)
[1] =>
int (2)
[2] =>
int (3)
}
[/PHP]
Line Comments
[PHP]
/ / comment
# comment
[/php]
Multi-line comments
[PHP]
/ *
comment
comment
* /
[/PHP]
It is a declaration of the variable.
[PHP]
$ a = 'string';
$ I = 1;
[/PHP]
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
I can check grammar in php command.
$ php -l hoge.php
There integer, floating-point to a number.
[PHP]
$ int = 100;
$ float = 100.123;
[/PHP]
[PHP]
$ I = 1 + 1;
$ I = 1 - 1;
$ I = 1 * 1;
$ I = 1/2;
[/PHP]
Quotient and remainder. Remove the integer part in the intval function after performing the division of normal order to obtain the quotient.
[PHP]
$ div = intval (3/2); / / quotient
$ mod = 3% 2; remainder / /
[/ PHP]
[PHP]
$ I + +; / / increment
$ i -; / / decrement
[/php]
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.
[PHP]
(\ t tabs [0x09]) / / abc cde; $ str1 = "abc \ Tcde"
$ str2 = 'abc \ Tcde'; (string \ t) / / abc \ tcde
$ Str3 = "$ str1 100" / / abc cde 100 / / $ str1 are deployed
when the string leads to a variable name enclosed in {} $ STR4 = "{$ str1} 100" / /
[/PHP]
[PHP]
/ / join
$ Join1 = 'aaa' 'BBB';.
$ Join2 = Implode (',', array ('aaa', 'BBB', 'ccc'));
/ / Split
$ split = explode (',', 'aaa, bbb, ccc');
/ / Length
('abcdef') $ length = strlen;
/ / (multi-byte) length
setting of / / internal encoding is necessary
/ / mb_internal_encoding ('UTF-8');
('tails ABC') $ mb_length = mb_strlen ;
/ / Cut
$ substr = substr ('abcd', 0, 2); / / ab
/ / Search
$ index = strpos ('abcd', 'bc'); / / Once you have located the position return false if you do not find (the first is 0),
[/PHP]
There are only associative array with PHP. Array is represented as an associative array of numbers is key. I also have the order.
[PHP]
$ array1 = array (1, 2, 3); (associative array key starts from 0) / / array
$ array2 = array ('a' => 1, 'b' => 2, 'c' = > 3); / / associative array
(1, 'a' => 1, 2) $ array3 = array; / / mixed also OK
[/PHP]
Assignment of a reference element
[php]
$i = $array1[0];
$s = $array2['a'];
[/php]
[php]
$array1[3] = 1;
$array2['z'] = 'zzz';
[/php]
The number of elements
[PHP]
$ len = count ($ array1);
[/PHP]
[PHP]
$ array = array (1, 2, 3);
retrieve the / / top
/ / $ first = 1 / $ array is, (2, 3); ($ array) $ first = array_shift
added to the / / top
array_unshift ($ array, 5); / / $ array is, (5, 2, 3)
to remove the / / end
$ last = array_pop ($ array); the array, / / $ last = 3 / $ (5, 2 )
Add to / / the end
array_push ($ array, 9); / / $ array is, (5, 2, 9)
[/PHP]
[php]
/ / obtain key
$ keys = array_keys ($ array);
/ / obtain value
$ values = array_values ($ array);
presence confirmation of / / key
$ boolean = array_key_exists ('key', $ array);
/ / Delete the key
unset ($ array ['key']);
[/PHP]
[PHP]
if (condition) {
}
[/PHP]
Notation such as the following will also be used when you described in HTML.
[PHP] hoge [/PHP]
[PHP]
if (condition) {
} else {
}
[/PHP]
Notation such as the following will also be used when you described in HTML.
[PHP] hoge foo [/ PHP]
elseif or else if is possible.
[PHP]
if (condition) {
} else {if
}
[/PHP]
Notation such as the following will also be used when you described in HTML.
[PHP] hoge foo [/PHP]
[PHP]
$ I = 0;
while ($ I <5) {
/ / process
$ I + +;
}
[/PHP]
Notation such as the following will also be used when you described in HTML.
[PHP] [/PHP]
[PHP]
for ($ I = 0; $ I <5; $ I + +) {
}
[/PHP]
Notation such as the following will also be used when you described in HTML.
[PHP] [/PHP]
I can handle each element of the associative array.
[PHP]
($ array as $ v) {foreach
value of the element / / $ v
}
{foreach ($ array as $ k => $ v)
The key of the element, $ v is the value of the element / / $ k
}
[/php]
Notation such as the following will also be used when you described in HTML.
[PHP] [/PHP]
There is a function with PHP. You can use the return to return the return value.
[PHP]
function sum ($ v1, $ v2) {
Return $ v1 + $ v2;
}
$ Total = sum (1, 2); / / $ Total = 3
/ / 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/202 = $ ret2
[/PHP]
There are several ways to file input and output.
Make the input and output files using the file pointer.
[PHP]
/ / read
$ fp = fopen ("/ path / to / File", "r");
if (! is_resource ($ fp)) {
Die ("Can not Open File");
}
while (feof ($ fp)!) {
$ line = fgets ($ fp, 4096);
something / / processing
}
fclose ($ fp);
/ / Write
$ fp = fopen ("/ path / to / File", "w");
if (! is_resource ($ fp)) {
Die ("Can not Open File");
}
fputs ($ fp, $ buff);
fclose ($ fp);
[/php]
It is stored in the array reads the entire file.
[PHP]
$ list = File ("/ path / to / File"); acquisition is an associative array of each line of the file / /
[/php]
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.
[PHP]
/ / read
to get the contents of the / / file; ("/ path / to / file") $ contents = file_get_contents
/ / Write
("/ path / to / file", $ buff) file_put_contents; / / to file write the contents of the $ buff
[/PHP]
In the following cases: in PHP, they are determined to be false.
== /! = In comparison operators such as, automatic conversion of numeric, string is performed. It may lead to results thus not intended.
[PHP]
var_dump (1 == 1); / / true
var_dump (1 == '1 '); / / true
var_dump (0 == 'a'); / / true
var_dump (100 '100A == '); / / true
var_dump ('+1' == '1 .0'); / / true
[/PHP]
In these cases, it is possible to compare exactly also the type of the variable === /! With ==.
[PHP]
var_dump (1 === 1); / / true
var_dump (1 === '1 '); / / false
var_dump (0 === '0 '); / / false
var_dump (100 === ' 100a '); / / false
var_dump ('+1' === '1 .0'); / / false
[/PHP]
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.
[PHP]
isset ($ a);
[/PHP]
You can use the $ argv variable to receive the command-line arguments.
[PHP]
var_dump ($ argv);
[/PHP]
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.
[PHP]
$ array = array (1,2,3);
$ mapped = array_map (create_function ('$ v', 'Return $ v * = 10;'), $ array);
[/PHP]
You can use the array_filter function, you can receive as a new associative array only the elements that match the conditions.
[PHP]
$ array = array (1,2,3,4);
$ filtered = array_filter ($ array, create_function ('$ v', 'Return ($ v> 2);');
[/PHP]
[PHP]
list ($ v1, $ v2, $ v3) = array (1, 2, 3);
[/PHP]
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.
[PHP]
('include_path', '. :/ path / to / libs') ini_set; to / / include_path setting the '. :/ path / to / libs'
[/PHP]
The current settings, I can check the php phpinfo command or function.
[PHP]
phpinfo ();
[/PHP]
# Output the setting value of all $ Php-i # I squeeze in grep $ Php-i | grep include_path
I can define a class in the class.
[PHP]
class User {
protected $ name = null;
public function __ construct ($ name) {
$ this-> name = $ name;
}
public function hello () {
printf ("!% s: Hello \ N", $ this-> name);
}
}
$ User = NEW User ('Mike');
$ user-> hello ();
[/PHP]
I can also be inherited. You can only single inheritance.
[PHP]
class MyUser extends User {
}
[/PHP]
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.
[PHP]
function foo () {
throw NEW Exception ();
}
TRY {
foo ();
} catch (Exception $ e) {
echo $ e-> GetTraceAsString ();
}
[/PHP]
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
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
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
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.