前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP基本语法快速入门

PHP基本语法快速入门

作者头像
Enjoy233
发布2019-03-05 11:21:08
1.8K0
发布2019-03-05 11:21:08
举报

PHP基础文法快速入门

Perl的和Ruby的语法组织得非常好,于是PHP也跟着尝试将这两种语言的优势引入。

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

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!

代码语言:javascript
复制
<?php $I = 1; hoge ($I); ?> 
<?php hoge (); ?>
打印语句print statement

I use the print / echo.

代码语言:javascript
复制
<?php 
"! Hello World" Print; 
"! Hello World" echo; 
?>

I often use var_dump () to debug. contents of the variable in var_dump () is output.

代码语言:javascript
复制
<?php 
$array = array (1,2,3); 
var_dump ($array);

?>

array (3) {  [0] =>  int (1)  [1] =>  int (2)  [2] =>  int (3)  } 

注释Comment

单行注释(Line Comments)

代码语言:javascript
复制
<?php 
//comment 
# comment 
?>

多行注释 Multi-line comments

代码语言:javascript
复制
<?php 
/ * 
comment 
comment 
* / 
?>
变量声明Variable declarations

It is a declaration of the variable.

代码语言:javascript
复制
<?php 
$a = 'string'; 
$I = 1; 
?>
脚本的执行(Execution of the script)

I run a PHP file on the command line.

代码语言:javascript
复制
$ php hoge.php 

You can also write directly to the PHP code. <? Php?> Is not required.

代码语言:javascript
复制
$ php-r "var_dump ('a');" 

Use the redirect to write the output to a file results.

代码语言:javascript
复制
$ php hoge.php> out 
词法检查(Grammar check of the script,lex)

I can check grammar in php command.

代码语言:javascript
复制
$php -l hoge.php 

2. 数值 Numerical 

Representation of the number

There integer, floating-point to a number.

代码语言:javascript
复制
<?php 
$int = 100; 
$float = 100.123; 
?>

4种算术操作 Four arithmetic operations

代码语言:javascript
复制
<?php 
$I = 1 + 1; 
$I = 1 - 1; 
$I = 1 * 1; 
$I = 1/2; 
?>

Quotient and remainder. Remove the integer part in the intval function after performing the division of normal order to obtain the quotient.

代码语言:javascript
复制
<?php 
$div = intval (3/2); //quotient 
$mod = 3 % 2; // remainder
?>
Increment and decrement
代码语言:javascript
复制
<?php 
$I + +; //increment 
$i --; //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.

代码语言:javascript
复制
<?php 
$str1 = "abc\tcde";   // (\ t tabs [0x09]) //abc cde;
$str2 = 'abc\tcde';  // (string \ t) abc \ tcde
$str3 = "$str1 100"; // abc cde 100 // $st1 will be replaced
$str4 = "{$str1} 100"; // when the string leads to a variable name enclosed in {}
?>
字符串操作String operation
代码语言:javascript
复制
<?php 
//join 
$Join1 = 'aaa' 'BBB';. 
$Join2 = Implode (',', array ('aaa', 'BBB', 'ccc'));
//Split 
$split = explode (',', 'aaa, bbb, ccc');
//Length 
$length = strlen;('abcdef') 
//(multi-byte) length 
setting of //internal encoding is necessary 
//mb_internal_encoding ('UTF-8'); 
$mb_length = mb_strlen('tails ABC');
//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), 
?>

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.

代码语言:javascript
复制
<?php 
$array1 = array (1, 2, 3); //array (associative array key starts from 0)
$array2 = array ('a' => 1, 'b' => 2, 'c' = > 3); //associative array (1, 'a' => 1, 2)
$array3 = array; //mixed also OK 
?>

Assignment of a reference element

代码语言:javascript
复制
<?php
$i = $array1[0];
$s = $array2['a'];
?>
<?php
$array1[3] = 1;
$array2['z'] = 'zzz';
?>

The number of elements

代码语言:javascript
复制
<?php 
$len = count ($array1); 
?>
Operation of the array
代码语言:javascript
复制
<?php 
$array = array (1, 2, 3); 
//retrieve thetop 
$first = array_shift ($array); //$first = 1, $array is, (2, 3); 
//added to thetop 
array_unshift ($array, 5); //$array is, (5, 2, 3) 
// to remove the end 
$last = array_pop ($array); //the array is (5, 2 ) , $last = 3;
// Add to the end 
array_push ($array, 9); // $array is, (5, 2, 9) 
?>
关联数组函数The function of associative array
代码语言:javascript
复制
<?php 
//obtain key 
$keys = array_keys ($array); 
//obtain value 
$values = array_values ($array); 
//key presence confirmation of 
$boolean = array_key_exists ('key', $array); 
//Delete the key 
unset ($array ['key']); 
?>

6. 控制语句Control statement

if statement
代码语言:javascript
复制
<?php 
if (condition) { 
} 
?>

Notation such as the following will also be used when you described in HTML.

代码语言:javascript
复制
<?php if (condition): ?>
<span>hoge</span>
<?php endif; ?>
if ~ else statement
代码语言:javascript
复制
<?php 
if (condition) { 
} else { 
} 
?>

Notation such as the following will also be used when you described in HTML.

代码语言:javascript
复制
<?php if(condition): ?>
  <span>hoge</span>
<?php else: ?>
  <span>foo</span>
<?php endif; ?>
if ~ else if statement

elseif or else if is possible.

代码语言:javascript
复制
<?php 
if (condition) { 
 else if{}
} 
?>

Notation such as the following will also be used when you described in HTML.

代码语言:javascript
复制
<?php if (condition): ?>
  <span>hoge</span>
<?php elseif (condition): ?>
  <span>foo</span>
<?php endif; ?>
while statement
代码语言:javascript
复制
<?php 
$I = 0; 
while ($I <5) { 
//process 
$I + +; 
} 
?>

Notation such as the following will also be used when you described in HTML.

代码语言:javascript
复制
<?php while ($i < 5): ?>
  <span><?php echo htmlspecialchars($i); ?></span>
  <?php $i++; ?>
<?php endwhile; ?>
for statement
代码语言:javascript
复制
<?php 
for ($I = 0; $I <5; $I + +) { 
} 
?>

Notation such as the following will also be used when you described in HTML.

代码语言:javascript
复制
<?php for ($i = 0 ; $i < 5 ; $i++): ?>
  <span><?php echo htmlspecialchars($i); ?></span>
<?php endfor; ?>
foreach statement

I can handle each element of the associative array.

代码语言:javascript
复制
<?php 
foreach($array as $v) { 
 //$v is value of the element
} 
foreach ($array as $k => $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.

代码语言:javascript
复制
<?php foreach ($array as $v): ?>
  <span><?php echo htmlspecialchars($v); ?></span>
<?php endforeach; ?>

7. 子进程Subroutine (function)

There is a function with PHP. You can use the return to return the return value.

代码语言:javascript
复制
<?php 
function sum ($v1, $v2) { 
Return $v1 + $v2; 
} 
$Total = sum (1, 2); //$Total = 3

//You can also return multiple values in an array 

代码语言:javascript
复制
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.

代码语言:javascript
复制
<?php 
//read 
$fp = fopen ("/ path / to / File", "r"); 
if (! is_resource ($fp)) { 
die ("Can not Open File"); 
}
while (feof ($fp)!) { 
$line = fgets ($fp, 4096); 
 //processing something
} 
fclose ($fp);
//Write 
$fp = fopen ("/ path / to / File", "w"); 
if (! is_resource ($fp)) { 
die ("Can not Open File"); 
}
fputs ($fp, $buff); 
fclose ($fp);
?>
file function

It is stored in the array reads the entire file.

代码语言:javascript
复制
<?php 
$list = File ("/ path / to / File"); // acquisition is an associative array of each line of the 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.

代码语言:javascript
复制
<?php 
//read 
$contents = file_get_contents("/ path / to / file")  // to get the contents of the file;
//Write 
file_put_contents("/ path / to / file", $buff); // write the contents of the file $buff 
?>

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.

代码语言:javascript
复制
<?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 
?>

In these cases, it is possible to compare exactly also the type of the variable === /! With ==.

代码语言:javascript
复制
<?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 
?>
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.

代码语言:javascript
复制
<?php 
isset ($a); 
?>
命令行参数 Command line arguments

You can use the $argv variable to receive the command-line arguments.

代码语言:javascript
复制
<?php 
var_dump ($argv); 
?>
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.

代码语言:javascript
复制
<?php 
$array = array (1,2,3); 
$mapped = array_map (create_function ('$v', 'return $v * = 10;'), $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.

代码语言:javascript
复制
<?php 
$array = array (1,2,3,4); 
$filtered = array_filter ($array, create_function ('$v', 'return ($v> 2);'); 
?>
多变量赋值Assignment to multiple variables
代码语言:javascript
复制
<?php 
list ($v1, $v2, $v3) = array (1, 2, 3); 
?>
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.

代码语言:javascript
复制
<?php 
ini_set('include_path', '. :/ path / to / libs');  //to include_path setting the '. :/ path / to / libs' 
?>

The current settings, I can check the php phpinfo command or function.

代码语言:javascript
复制
<?php 
phpinfo (); 
?>
代码语言:javascript
复制
# Output all the setting value

$ php -i

# grep in

$ php -i | grep include_path

Class definition

I can define a class in the class.

代码语言:javascript
复制
<?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 (); 
?>

I can also be inherited. You can only single inheritance.

代码语言:javascript
复制
<?php 
class MyUser extends User { 
} 
?>
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.

代码语言:javascript
复制
<?php 
function foo () { 
throw NEW Exception (); 
}
try { 
foo (); 
} catch (Exception $e) { 
echo $e-> 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.

代码语言:javascript
复制
 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.

Related entries

源地址:

http://www.1x1.jp/blog/2010/01/php-basic-syntax.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013年05月31日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Version
  • 1. Basis
    • 代码块Code block
      • 打印语句print statement
        • 注释Comment
          • 变量声明Variable declarations
            • 脚本的执行(Execution of the script)
              • 词法检查(Grammar check of the script,lex)
              • 2. 数值 Numerical 
                • Representation of the number
                  • Increment and decrement
                  • 3. 字符串 String
                    • String representation
                      • 字符串操作String operation
                      • 4. 数组与关联数组Array, associative array
                        • Operation of the array
                          • 关联数组函数The function of associative array
                          • 6. 控制语句Control statement
                            • if statement
                              • if ~ else statement
                                • if ~ else if statement
                                  • while statement
                                    • for statement
                                      • foreach statement
                                      • 7. 子进程Subroutine (function)
                                      • 8. 文件的输入输出Input and output files
                                        • fopen function
                                          • file function
                                            • file_get_contents function / file_put_contents function
                                            • You should know is good grammar
                                              • Truth-value
                                                • == And ===
                                                  • Whether a variable is defined
                                                    • 命令行参数 Command line arguments
                                                      • array_map
                                                        • array_filter
                                                          • 多变量赋值Assignment to multiple variables
                                                            • php.ini
                                                              • Class definition
                                                                • Exception
                                                                • PHP参考资料
                                                                  • Official Manual
                                                                    • Coding conventions
                                                                      • Modern PHP
                                                                        • Framework
                                                                        领券
                                                                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档