php crash _day1

I. Overview

1. What is PHP

  • PHP (Hypertext Preprocessor), English hypertext preprocessing language acronym. PHP is a cross-platform, description language embedded server-side implementation of the HTML document embedded scripting language which is performed on the server side, the language style is similar to the C language.

  • Cross-platform: development and use of windows macos linux in a variety of operating system platforms

  • Embedded: You can mix PHP developers connect to the database and HTML document data interaction

  • PHP development web suffix is ​​.php. PHP plus Mysql database language combination is the best combination of web application development with open source, free, and easy to learn simple, efficient, object-oriented, practical advantages.

  • Open Source: Public open source developers around the world can be maintained and developed

  • Free: software development using PHP, without having to pay additional fee

  • Rapid Development: development of network applications fast speed, low cost of learning, with a large number of functions and associated method for processing operations

2, PHP can be used to do what

PHP server-side data development. Backend web application development.

PHP can meet most of the business scenario development.

  • Website development
  • API interface development
  • Internal OA System Development
  • Mobile end background development
  • game development
  • Server development
  • Advertising System Development

① Baidu search page platform to showcase

②PHP can be achieved Login or register related functions

3, PHP website development project known

  • FaceBook (the world's largest social networking site)
  • Taobao (China's largest electricity provider platform)
  • Yahoo (before 2007 the largest search engine)
  • Sina (well-known media sites)
  • NetEase (well-known media sites)
  • Tencent (well-known media sites)
  • Baidu (currently the largest search engine)
  • Zhongguancun Online (the nation's largest hardware evaluation platform)

Second, the preparation before PHP development

1, PHP environment Introduction

① PHP development environment to develop applications relevant code

WAMP W(Windows)+A(Apache)+M(Mysql)+P(PHP)

Environmental transport real-dimensional environment of PHP applications running projects ② online environment

LAMP L(Linux)+A(Apache)+M(Mysql)+P(PHP)

Deployment environment:

① separately mounted, the component-related environmental software separately installed and configured

② integrated installation by a certain kind of software package installed directly above ambient

phpstudy wamp xampp

Difference: integrated installation is simple and fast installation configuration, separate, component-based custom installation of some more strengthening.

2, phpstuday the installation

Recommended that all contents of this operation in windows10 virtual machine

① decompress the installation package using the installation

② extracted to the specified location

③ Click Start PHP-related environment

Third, the process site page views

1, page type

① static pages

Page content is not going to change, it is the HTML page. Ending in .html .htm, etc.

② dynamic pages

Will go through the implementation of server-side script, connect to the database for data processing and other operations, perform resolves to a static page. And then return to a static page from the web server to the browser. Ending in .php, etc.

2, PHP location of its role

① If the access is a static html page and return directly from the web server (Apache)

② If the visit is a dynamic page PHP, web server (Apache) will first load the PHP parsing the page, the relevant data processing operations. Analytical called static html page. Then returned to the user browser by a web server

③ the page to access the site, the network protocol usually used is http or https (http encryption protocol, more secure), http protocol is the default port 80, Apache runs on port 80.

Four, PHP basic syntax

1, the start and end tag syntax

When writing PHP, we need to declare a tag. In this tag, we need to be parsed by PHP. Code in this tag, all PHP syntax code.

PHP code files, the general requirements are carried out at the end of .php.

Grammar rules:

<?php
PHP相关的代码
?>
①尾部结束标记是可以省略的
  • To end
  • Each line of code in PHP must end with a semicolon, the semicolon is a separator, for the instruction set distinguish.
  • Programming languages ​​are executed sequentially from top to bottom.

2, instruction delimiter

After the end of each line of code, are to; for end

If only one line is the last line of the statement or statements, can be omitted. But it is recommended to write each row.

3, echo output statement

echo output syntax is a structure in PHP. Could you want displayed on the browser page content output through it.

4, PHP and HTML relationships

Mixed-use development mode:

① mixed PHP code in html

If want to write PHP code in HTML, the need to expand the name of the HTML page, modify the .php from the HTML. To parse the PHP code.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>php</title>
</head>
<body>
  1
  我爱PHP
  <?php echo 'i love php'?>
</body>
</html>

② output HTML code in PHP code

<?php
echo '1<br />';
echo '<h1>i love php</h1>';

PHP and HTML relationships:

  • php only exist on the server side, all we see through a browser, all HTML (CSS JS and other client side language)
  • PHP is doing the role of the site in accordance with our needs is to output the correct HTML code
  • For PHP, all the HTML code are strings

5, annotation mode

Some general comments are described in character codes. Code for illustrative purposes, a simple and functional description.

Good code, a good programmer should have the habit of writing comments. Get into the habit of writing explanatory notes.

Annotated code will not be executed.

Notes ways:

① single-line comments

//需要注释的代码
#需要注释的代码  类unix语法

② multi-line comments

/*
*需要代码或者说明的内容
*
*
*
*/

6, header response header

header can tell the browser what way parsing code. It can be used to solve the Chinese garbled situation.

Chinese garbled phenomenon:

<?php
echo '我爱PHP'; //浏览器访问这个页面会出现乱码    
?>

Solution:

<?php
//使用header响应头声明,告知浏览器以utf8的编码格式显示输出内容
header('Content-Type:text/html;charset=utf8');
echo '我爱PHP'; //浏览器访问这个页面会  我爱PHP    
?>

7, the definition of variables

Variable information may be stored, storing information as a container.

Variable in the code is running and operation, can make changes and variations.

PHP variable to $ (keyboard shift + 4 dollar dollar sign)

Naming rules:

  • ** variable name must be a letter or an underscore "_" at the beginning **
    • Right: $ abc123
    • Error: $ 123abc
  • Variable names can only contain alphanumeric characters and the underscore
    • Right: $ abc_dfe
    • Error:! $ @ # Abc
  • Variable names can not contain spaces . If a variable name consists of several words, then they should be separated with an underscore (for example, $ my_string), or start with a capital letter (for example, $ myString)
    • Right: $ user_name
    • Error: $ user name
  • $ This is a keyword can not be used. System reserved word

8, the data type of the variable

In accordance with the basic rules of grammar PHP, we can define variables, variables among eight kinds of data types, variables, and variables can undertake some operations. The value of the variable you can directly assign, assigned address, or URL can also be received from the form to.

PHP has eight data types:

  • Integer Integer

  • Float Float

  • Boolean Boolean

  • String String

  • Array Array

  • Object Object

  • Resource Resource

  • Empty type NULL

Scalar type

  • int int

  • boolean Boolean

  • string string

  • float float

Composite type

  • array array

  • Object object

Special type

  • resource Resources
  • NULL]

Q: How do I view the data type of a variable?

A: You can var_dump PHP syntax.

9, access to a range of variables

When after variable assignments need to be used, to pay attention to the scope of its use. Variables in this range, may be used to. Other than using the range. This range is called the "scope of the variable."

Variables in PHP and JS variables in a big difference:

  • JS access to a range of variables
    • JS variables into the global and local variables
    • In the global scope, you can access the global variables
    • In the local scope, access to local scope
    • In the local scope can access the global variables
  • Access PHP variable scope
<?php
$str = 'hello PHP';
funcation sayhello(){
    echo $a; //是否可行?
}
sayhello();
?>

solution:

It can be accessed by using the variable become the way of global variables.

<?php
$str = 'hello PHP';
// echo $str;
// 函数
function sayhello()
{
  global $str;
  // 是否可以使用到str
  echo $str;
}
// 调用函数
sayhello();

10, a variable assignment

Variable transmission mode values:

  • Value transfer

    • When the new value of the modified variable, the value of the original variable will not be changed out.
    <?php
    // 变量赋值
    $name = 'tom';
    $new_name = $name;
    echo $new_name;
    ?>
  • Passed by reference

    • Will variables corresponding memory address values ​​passed in the past, when the value of the variable is changed, the value of the original variable will be changed out.
    <?php
    $a = 3;
    // 引用传值方式
    $b = &$a;
    // $b发生改变之后
    $b = 5;
    echo $b;  //$b 5
    echo '<hr>';
    // 问$a,现在等于几  5
    echo $a;

11, single and double quotes distinction

You can use either a single statement quoted string in PHP which you can use double quotes. But the single and double quotes are there are some differences.

<?php
// 输出hello php
// 查看单引号和双引号的区别
$str = 'hello';
// 单引号
echo '$str php';
echo '<hr>';
// 双引号
echo "$str php";

Through the above cases the code, you can know that will not be resolved when the variable in single quotes Php in, because they were considered to be a string. Double quotes variables will first be resolved.

In the actual development work which, if not need to parse a variable, just type a string assignment statement, it should use single quotes, efficiency will be higher.

The difference between single quotes and double quotes

In php, may be declared String character-type data by single or double quotation marks, but both will be slightly different when parsing variables:

  • Double quotes may be made variable by the normal resolved php parser.
  • Single quotes in variables can not be resolved, only the variable output directly as a normal string

12, constant use

A constant is an identifier (name) a simple value. As the name implies, during the execution of the script that value can not be changed.

Constant default is case-sensitive. By convention constant identifiers are always uppercase.

Constant name and any other PHP tags follow the same naming rules. A valid constant name with a letter or underscore, followed by any letters, numbers or underscores.

Constants do not need to use the $ symbol.

grammar:

define(‘大写的常量名称’,常量值);

Small text columns: Geometric mathematics, the area is calculated by the radius of a circle.

The square of the radius of the circle circumference ratio *

Pi is constant, so it can be carried out using constant definitions.

<?php
// 通过圆的半径计算面积
// 常量的使用
// 在计算过程中不需要进行改变,所以可以设定为常量
define('PI', 3.14);
// define('PI', 3.1415926);
// 计算圆的面积
echo PI * 5 * 5;

13, the operator

① Arithmetic Operators

+ Plus

-Less

* Multiplication

/ Divisible

% Remainder Remainder division number

<?php
$a = 10;
$b = 5;
// 加
echo $a+$b;
echo '<hr>';
// 减
echo $a-$b;
echo '<hr>';
// 乘
echo $a*$b;
echo '<hr>';
// 整除
echo $a/$b;
echo '<hr>';
// 求余
echo $a%$b;

② from Canada and decrement operators

For example: There is a variable $ i

$ I ++ first operation, the operation itself plus 1

++ $ i add 1 to the operation itself, the operation

$ I-- first operation, the operation itself minus 1

- $ i decremented to itself, after calculation

<?php
$a = 5;
// 先运算,后自加
echo $a++;
echo '<hr>';
// 这里由于$a已经变为6
// 先自加,后运算
echo ++$a;

③ comparison operators

> Greater than

> = Greater than or equal

<Less than

<= Less than or equal

== equal to the value equal to

=== congruent constant equal to (the same value and the same type)

! == constant is not equal

<?php
// header响应头设置
header('Content-Type:text/html;charset=utf8');
// 比较运算符
// 整型
$a = 5;
// 字符串类型
$b = '5';
// if($a > $b){
//   echo '$a 大于$b';
// }else{
//   echo '$a 小于$b';
// }
if($a == $b){
  echo '$a和$b的值是相等的';
}
echo '<hr>';
if($a === $b){
  echo '$a和$b的值和类型都是相等的';
}

④ logical operators

&& and with a plurality of conditions are met

|| or conditions to one or

! Negate the value of the reverse operation condition

⑤ String operators (operator connection)

In '' is a string of splicing operation

<?php
// header
header('Content-Type:text/html;charset=utf8');
$a = 5;
$b = 3;
// 判断$a和$b是否同时大于4;
// if($a > 4 && $b >4 ){
//   echo '$a和$b都大于4';
// }
// if($a > 4 || $b >4 ){
//   echo '$a和$b有大于4的';
// }
if(!$a > $b){
  echo '$a大于$b';
}

⑥ assignment operator

= += -= /= %=

$ A = 10; 10 assigned to the variable $ a

<?php
$str = 'hello';
echo '$str PHP';
echo '<hr>';
// 字符串拼接方式
echo $str.' PHP';

14, operator precedence

Will perform an arithmetic operation according to the priority of the operators in PHP, if you want to execute, may also be used () line for performing operations.

<?php
// 运算符的优先级
$a = 5;
$b = 4;
// 问结果是多少
// ①5+4*5 = 25
echo $a+$b*$a;
echo '<hr>';
// 括号里的优先运算
echo ($a+$b)*$a;
<?php
// header响应头信息
header('Content-Type:text/html;charset=utf8');
// 运算优先级
$a = 5;
$b = 4;
// ①$a--等于4
// ②$b--等于3
// ③判断之后的$a和$b是否相等
// $a-- == $b--;
// echo $a;
// echo '<hr>';
// echo $b;
if(--$a == $b){
  echo '值相等';
}

15, selection structure

①if elseif else

grammar:

if(判断条件){

如果满足以上的判断条件,需要执行的代码操作

}elseif(判断条件){

需要执行代码操作

}else{

代码操作

}
<?php
// header响应头
header('Content-Type:text/html;charset=utf8');
// 数字星期几
$day = 3;
if($day == 1){
  echo '星期一';
}elseif($day == 2){
  echo '星期二';
}elseif($day == 3){
  echo '星期三';
}elseif($day == 4){
  echo '星期四';
}elseif($day == 5){
  echo '星期五';
}else{
  echo '周末';
}

②switch

grammar:

switch(条件[变量]){
case: 等于的值
需要执行的代码
break;
case:
需要执行的代码
break;
defalut:
}
<?php
// header响应头
header('Content-Type:text/html;charset=utf8');
// 数字星期几
$day = 3;
switch ($day) {
  case '1':
    echo '星期一';
    break;
  case '2':
    echo '星期二';
    break;
  case '3':
    echo '星期三';
    break;
  case '4':
    echo '星期四';
    break;
  case '5':
    echo '星期五';
    break;
  default:
    echo '周末';
    break;
}

Q: if the difference ... elseif .... else structure and switch structure **?

A: Both syntaxes may be selected structures are judged. However, if the structure can judgment condition, the determination condition may be equal to a fixed value. switch can only be a fixed value.

16, the cyclic structure

Loop: Repeat doing the same thing.

For example, you want to output digital 1-9.

Before the wording:

echo 1;

echo 2;

echo 3;

.....

①for structure

grammar:

for($i=起始值;$i<循环条件止;$i++){
执行的代码
}
<?php
// for循环
// for ($i=1; $i < 10; $i++) {
for ($i=1; $i <=9; $i++) {
  echo $i.'<hr>';
}

②while structure

grammar:

注意定义循环条件的起始值
while(循环条件){
执行的代码
}
<?php
// while循环
// 定义while条件的起始值
$i = 1;
while ($i<= 9) {
  echo $i.'<hr>';
  // 执行一次进行+1操作;
  $i++;
}

③do ... while structure

grammar:

do{
 执行的代码
}
while(判断条件);
<?php
// do...while循环
$i = 1;
// 先执行一次操作
do{
  echo $i.'<hr>';
  // 执行之后给$i递增
  $i++;
  // 再会进行判断
}while($i<=9);

Q: for and while loops difference?

A: for structure suitable to know the number of cycles Accordingly, while an unknown number of cycles can

Q: The difference between while and do ... while loop of?

A: After a while loop to execute judgment, do ... while loop will execute once before, and then judge

17, the circulation of the two keywords

①continue interrupt the current cycle, to perform the subsequent cycle, the current cycle used to skip the rest of the code in the loop structure and when the condition evaluates to true, the next time through the loop start

<?php
// 循环输出1-9,不输出6
for ($i=1; $i <=9 ; $i++) {
  // 判断等于则停止此次循环
  // 继续之后的循环
  if($i == 6){
    continue;
  }
  echo $i.'<hr>';
}

②break end execution of the current for, while, do-while, or switch structure, out of the entire cycle

<?php
// 循环输出1-9,不输出6
for ($i=1; $i <=9 ; $i++) {
  // 判断等于则停止此次循环
  // 继续之后的循环
  if($i == 6){
    break;
  }
  echo $i.'<hr>';
}

Five, PHP arrays in

1, the role of the array

I want to use a variable to store a person's information.

Name, age, position

<?php
 //使用变量
 //存储一个人的信息
 //姓名,年龄,职位
$name = 'xiaoming';
$age = 24;
$job = 'PHP'

The above structure has a certain relationship, if you simply use a variable to store, not too appropriate. You can use an array of ways.

A set of data has a relationship.

2, the definition array

PHP in the array is an ordered map. A map is a map values ​​to keys. (Key => value)

The nature of the array is to store, manage and operate a set of variables. A data type, an array of eight kinds of PHP is provided, belonging to the complex data type.

Role and characteristics of the array:

① can put different types of variables in the same array

② array capacity can be automatically adjusted according to increase or decrease the number of elements of

Define an array in PHP is very flexible, size when you create the array not need to specify an array, do not need to declare an array before use, you can store any type of data in the same array

Custom arrays can use the following two methods:

  • Direct assignment to declare the array as an array element
  • Use array () function declare an array

When the above two methods declare arrays, not only can specify a value of the element, the element can also specify the index.

grammar:

① indexed array

array(元素,元素,元素)

A set of numbers stored in the array 1-5

<?php
$array = array(1,2,3,4,5);
var_dump($array);
?>

② associative array

array(key=>value,key=>value,key=>value)

key is not a number

3, access array elements

① indexed array

Access the elements of an array by index access, pay attention to the subscript starts at 0

<?php
$array = array(1,2,3,4,5);
var_dump($array);
echo '<hr>';
//输出数组的元素
//数组元素的下标从0开始
echo $array[2];  //输出3
?>

② associative array

Access by key elements of the array access

<?php
//关联数组
$person = array(
'name' => 'xiaoming',
'age' =>  23,
'job' => 'PHP'
);
var_dump($person);
echo '<hr>';
echo $person['name'];
//输出 xiaoming

4, traverse the output array

① through the recycle ratio for traversing

<?php
$array = array(1,2,3,4,5);
var_dump($array);
echo '<hr>';
//输出数组的元素
//数组元素的下标从0开始
echo $array[2];
echo '<hr>';
//遍历索引数组
for ($i=0;$i < 5;$i++){
    echo $array[$i].'<br />';
}
?>

②foreach traversal

In general, we recommend greater use foreach to traverse the array. foreach is the result of system optimization, efficiency is more higher.

<?php
//关联数组
$person = array(
'name' => 'xiaoming',
'age' =>  23,
'job' => 'PHP'
);
var_dump($person);
echo '<hr>';
echo $person['name'];
echo '<hr>';
//foreach遍历
foreach ($person as $key => $value){
    echo $key.':'.$value.'<br />';
}
echo '<hr>'
foreach ($person as $value){
    echo $value.'<br />';
}

Guess you like

Origin www.cnblogs.com/jiaxiaozia/p/12323487.html