Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【20161202】PHP学习笔记(一) #54

Closed
zhongxia245 opened this issue Dec 2, 2016 · 0 comments
Closed

【20161202】PHP学习笔记(一) #54

zhongxia245 opened this issue Dec 2, 2016 · 0 comments

Comments

@zhongxia245
Copy link
Owner

时间:2016-12-02 10:30:13

有前端两三年的基础,也有后端(Java,.NET,SQL等常规技术)的基础,然后了解下PHP最基本的使用,至少能够快速编写接口,套用模板等。

零、和其他语言的不同点

  • 关键字不区分大小写,变量,函数名区分大小写
  • 类型松散的语言,不需要声明变量的数据类型
  • 内置非常多的函数
  • 字符串相加 使用 . , 使用 点, 而不是 + 号,注意
  • if .. else , if..elseif...else ,注意是 elseif
  • for循环 for($arr as $item) 数组在前
  • 定义常量的方式有点不一样,需要使用 define 来声明

一、PHP常规知识点

1. php代码块

代码放在 <?php ?> 代码块里面,文件以 .php 结尾

<?php
    echo "hello world"
?>

2. 声明变量

PHP是类型松散型的语言,不需要类似C# ,Java 声明指定的数据类型
但是变量 必须 以 $ 开头

$num = 100
$str = "zhongxia"
$pid = 3.14
$arr = 

3. echo 和 print

echo 和 print 功能一样, 输出内容

  • echo 可以输出一行或者多行字符串
  • print 只能输出一行字符串

4. 数据类型

和其他语言一样,

  • 整型(-1 , 2 ,4)
  • 字符串(单引号,双引号 包起来都可以)
  • 浮点型(0.1 ,1.1)
  • 布尔型(true,false)
  • 数组
    • 定义常规数组
      $arr = array("zhongxia","age","test")
    • 定义对象数组
    //使用指定键  =>
    $arr = array("name"=>"zhongxia","age"=>18)
    
    //使用常规方式
    $arr["name"] = "zhongxia"
    #arr["age"] = "18"
    
    

5. 运算符

和其他语言一样,没有什么特殊的

+ - * /  %
==  
=== 
!= 
+= 
-=
...

6. 判断语句 (if..elseif...else, switch)

主要不同点是 elseif

可以用 and ,or , && , ||

and == &&
or == ||

if($x >1 && $x < 10){
    //...
}elseif($x>10 && $x <100){
    //...
}else{
    //...
}


//swich
switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}

7. 循环

for, while ,do..while, foreach

$i = 0;
for($i; $i<10; $i++){
    echo '循环次数' . $i
}

while($i<10){
    echo 'while循环' . $i
    $i++;
}

$arrs = array("red","green","blue","yellow"); 

foreach ($arrs as $value) {
  echo "$value <br>";
}

8. 定义常量

定义常量 和 其他的C#,java 不太一样
需要使用 define 来定义

//第一个参数就是常量的名称
define("GREETING", "Welcome to W3School.com.cn!");
echo GREETING;

//常量名不区分大小写, 第三个参数使用 true
define("GREETING", "Welcome to W3School.com.cn!", true);
echo greeting;

9. 函数, 和JavaScript一样

//函数的定义,设置参数,默认参数
function say($name,$age=18){
    echo 'this is a function! $name'
}

10. 超全局变量

就是PHP内置的一些全局变量,不需要自己定义。 使用他们也不需要引入任何东西,直接开始用就行。

$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

二、学习网站

  1. 《w3school PHP教程》
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant