PHP language structure

不言
Release: 2023-03-24 17:50:01
Original
1760 people have browsed it

这篇文章介绍的内容是关于PPHP语言结构,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下



1、内置函数

<?phpecho rand();        //返回随机整合echo "\n";      
echo rand(1000,9999);        //产生一个四位随机整数?>
Copy after login

第一次运行结果:
PHP language structure
第二次运行结果:
PHP language structure


2、自定义函数
①自定义和调用函数

<?phpfunction say($customer){    //自定义函数say
    return $customer.",欢迎您来到假日酒店";
}echo say(&#39;张先生&#39;);      //调用函数say?>
Copy after login

PHP language structure
②向函数传递参数值

<?phpfunction total($days,$roomprice){     //声明自定义函数
    $totalcost=$days*$roomprice;      //计算住宿总费用
    echo "需要支付的总价为".$totalcost."元。";    echo "\n";
}$days=10;      //声明全局变量$roomprice=100;
total($days,$roomprice);      //通过变量传递参数total(5,13);        //直接传递参数值?>
Copy after login

PHP language structure


3、条件控制结构
①单一条件分支结构(if)

<?php$num=rand(1,100);if($num % 2 !=0){    echo "\$num=$num";    echo "\n";    echo $num."是奇数";
}?>
Copy after login

PHP language structure
②双向条件分支结构(if…else)

<?php$num=rand(1,100);if($num % 2 !=0)    echo $num."是奇数";else 
    echo $num."是偶数";?>
Copy after login

PHP language structure
③多向条件分支结构(switch语句)

<?php$x=5;switch ($x)
{    case 1:        echo "数值为1";            break;    case 2:        echo "数值为2";        break;    case 3:        echo "数值为3";        break;    default:        echo "数值不在1-3之间";
}?>
Copy after login

PHP language structure


4、循环控制结构
①while循环语句

<?php$num=1;			//定义变量$num$str="20以内的奇数为:";   //定义变量$strecho $str;while($num<=20){			//判断$num是否小于等于20
    if($num % 2 !=0){		//判断$num是否为奇数,为奇数则输出,否则做加一操作
echo $num."\t";
    }    $num++;
}   
?>
Copy after login

PHP language structure
②do…while循环语句
其中先执行do后面的”命令执行语句”,其中的变量会随着命令的执行发生变化,当此变量通过while后面的条件判断为false时,停止执行“命令执行语句”。

<?php$aa=0;			//定义变量$numwhile($aa !=0){			//判断$num是否小于等于20
    echo "不会被执行的内容";
}do{    echo "被执行的内容";
}while($aa !=0);?>
Copy after login

PHP language structure
③foreach循环语句

foeeach(数组 as 数组元素){
对数组元素的操作命令;
}
Copy after login
foreach(数组 as 数组元素值){
对数组元素的操作命令;
}
Copy after login
<?php$arr=array("one","two","three");foreach($arr as $value)    //使用foreach循环输出{    echo"数组值:".$value;    echo "\n";
}?>
Copy after login

PHP language structure

相关推荐:

PHP中字符串与正则表达式

The above is the detailed content of PHP language structure. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!