Table of Contents
PHP语法基础,php语法
Home php教程 php手册 PHP语法基础,php语法

PHP语法基础,php语法

Jun 13, 2016 am 08:51 AM
php

PHP语法基础,php语法

1.PHP语法基础
  • $a = 10; 变量 可以在运行过程中修改
<span>$a</span> = 10<span>;
</span><span>$a</span> = 20<span>;
</span><span>$b</span> = 5<span>;
</span><span>echo</span> <span>$a</span>+<span>$b</span>;
Copy after login
  • const THE_VALUE = 100; 常量 只能被赋值一次,否则会报语法错误
<span>const</span> THE_VALUE = 100<span>;
</span><span>//</span><span>THE_VALUE = 200; 有语法错误 运行时会报错</span>
<span>echo</span> THE_VALUE;
Copy after login
  • 在PHP4中定义常量的方法
<span>define</span>('THE_VALUE',200<span>);
</span><span>//</span><span>THE_VALUE = 200; 有语法错误</span>
<span>echo</span> THE_VALUE;
Copy after login

2.函数 包装了很多功能的代码块
  • 简单函数
<span>function</span><span> traceHelloPHP(){
    </span><span>echo</span> 'Hello PHP'<span>;
    </span><span>echo</span> '<br/>'<span>;
    </span><span>echo</span> 'Hello World'<span>;
}
traceHelloPHP();</span>
Copy after login
  • 将函数作为其它函数的回调函数
<span>function</span><span> traceHelloPHP(){
    </span><span>echo</span> 'Hello PHP&mdash;&mdash;back'<span>;
    </span><span>echo</span> '<br/>'<span>;
    </span><span>echo</span> 'Hello World'<span>;
}
</span><span>//</span><span>traceHelloPHP();</span>
 
<span>$func</span> = 'traceHelloPHP'<span>;
</span><span>$func</span>();
Copy after login
  • 函数的传入参数
<span>function</span> sayHello(<span>$name</span><span>){
    </span><span>echo</span> 'Hello '.<span>$name</span><span>;
    </span><span>echo</span> '<br/>'<span>;
}
sayHello(</span>'ZhangSan'<span>);
sayHello(</span>'Lisi');
Copy after login
  • 多个传入参数的函数
<span>function</span> traceNum(<span>$a</span>,<span>$b</span><span>){
    </span><span>echo</span> 'a = '.<span>$a</span>.' , b = '.<span>$b</span>.'<br/>'<span>;
    </span><span>echo</span> "a = <span>$a</span> , b = <span>$b</span><br/>";  <span>//</span><span>这样写也是可以的</span>
<span>}
traceNum(</span>2,3);
Copy after login
  • 带有返回值的函数
<span>function</span> add(<span>$a</span>,<span>$b</span><span>){
    </span><span>return</span> <span>$a</span>+<span>$b</span><span>;
}
</span><span>echo</span> add(10,2).'<br>';
Copy after login

3.流程控制
  • 出现乱码的原因:服务器的编码方式和客户端的编码方式不同 解决办法:添加html头,并将php代码嵌入body中
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Control Flow</title>
</head>
<body>
<?<span>php
</span><span>/*</span><span>*
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/10
 * Time: 16:07
 </span><span>*/</span>
<span>function</span> getLevel(<span>$score</span><span>){
    </span><span>if</span>(<span>$score</span> >= 90<span>){
        </span><span>return</span> '优秀'<span>;
    }</span><span>elseif</span>(<span>$score</span> >= 80<span>){
        </span><span>return</span> '良好'<span>;
    }</span><span>elseif</span>(<span>$score</span> >= 70<span>){
        </span><span>return</span> '好'<span>;
    }</span><span>elseif</span>(<span>$score</span> >= 60<span>){
        </span><span>return</span> '可以'<span>;
    }</span><span>else</span><span> {
        </span><span>return</span> '差'<span>;
    }
}
</span><span>echo</span> getLevel(92).'<br/>'<span>;
</span>?>
</body>
</html>
Copy after login
  • 控制语句写法2
<span>function</span> getLevel_switch(<span>$score</span><span>){
    </span><span>switch</span>(<span>$score</span>/10<span>){
        </span><span>case</span> 10:
        <span>case</span> 9:
            <span>return</span> '优秀'<span>;
        </span><span>case</span> 8:
            <span>return</span> '良好'<span>;
        </span><span>case</span> 7:
            <span>return</span> '好'<span>;
        </span><span>case</span> 6:
            <span>return</span> '可以'<span>;
        </span><span>default</span>:
            <span>return</span> '差'<span>;
    }
}
</span><span>echo</span> getLevel_switch(60).'<br/>';
Copy after login
  • 控制语句写法3
<span>function</span> getLevel_break(<span>$score</span><span>){
    </span><span>$result</span> = '--'<span>;
    </span><span>switch</span>(<span>intval</span>(<span>$score</span>/10<span>)){
        </span><span>case</span> 10:
        <span>case</span> 9:
            <span>$result</span> = '优秀';<span>break</span><span>;
        </span><span>case</span> 8:
            <span>$result</span> = '良好';<span>break</span><span>;
        </span><span>case</span> 7:
            <span>$result</span> = '好';<span>break</span><span>;
        </span><span>case</span> 6:
            <span>$result</span> = '可以';<span>break</span><span>;
    }
    </span><span>return</span> <span>$result</span><span>;
}
</span><span>echo</span> getLevel_break(83).'<br/>';
Copy after login

4.循环
  • for循环
<span>for</span>(<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>){
    </span><span>echo</span> 'Hello '.<span>$i</span>.'<br/>'<span>;
}</span>
Copy after login
  • while循环 先判断再执行
<span>$i</span> = 0<span>;
</span><span>while</span>(<span>$i</span> < 50<span>){
    </span><span>echo</span> 'Hello '.<span>$i</span>.'<br/>'<span>;
    </span><span>$i</span>++<span>;
}</span>
Copy after login
  • do-while循环 先执行在判断
<span>$i</span> = 0<span>;
</span><span>do</span><span>{
    </span><span>echo</span> 'Hello '.<span>$i</span>.'<br>'<span>;
    </span><span>$i</span>++;   <span>//</span><span>一定不要忘记自增,否则就会编程死循环</span>
}<span>while</span>(<span>$i</span><20);
Copy after login
  • break跳出 *整个* 循环
<span>for</span>(<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>){
    </span><span>echo</span> 'Hello '.<span>$i</span>.'<br/>'<span>;
    </span><span>if</span>( <span>$i</span> == 20<span> ){
        </span><span>break</span><span>;
    }
}</span>
Copy after login
  • continue 跳出 *本次* 循环
<span>for</span>(<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>){
    </span><span>echo</span> 'Hello '.<span>$i</span>.'<br/>'<span>;
</span><span>//</span><span>    if( $i == 20 ){
//        break;
//    }</span>
    <span>if</span>(20 == <span>$i</span><span>){
        </span><span>continue</span><span>;
    }
    </span><span>echo</span> 'Run here '.<span>$i</span>.'<br/>'<span>;
}</span>
Copy after login

5.逻辑运算 与&& 或|| 非!
<span>function</span><span> traceNum(){
    </span><span>for</span>(<span>$i</span>=0;<span>$i</span><=100;<span>$i</span>++<span>){
</span><span>//</span><span>        if(0 == $i%2 && 0==$i%3){
//            echo $i.'<br/>';
//        }
//        if(0 == $i%2 || 0==$i%3){
//            echo $i.'<br/>';
//        }</span>
        <span>if</span>(!(0 == <span>$i</span>%2<span>)){
            </span><span>echo</span> <span>$i</span>.'<br/>'<span>;
        }
    }
}
traceNum();</span>
Copy after login

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles