PHP学习笔记之变量用法详解
变量用于存储值,比如数字、字符串或函数的结果,这样我们就可以在脚本中多次使用它们了变量用于存储值,比如数字、文本字符串或数组。
一旦设置了某个变量,我们就可以在脚本中重复地使用它。
PHP 中的所有变量都是以 $ 符号开始的。
PHP的代码插入很直观,"" 结束。
所有的变量以"$"开头,例如"$money"。(定义符容易让人遐想)需要使用时,定义即可,可以省略对变量类型的定义,php的编译器会自动为我们选择的。
对于条件判断语句,php和c差不多。if(条件A){过程A}即可,还有就是else和elseif。else用于运行条件A以外的过程,elseif(条件N....)则是其他条件为真时运行的代码。
下面是自己写的代码:
代码如下 | 复制代码 |
if.php $sex = $_POST["sex"]; if($sex=="male") { echo "男性"; } elseif($sex=="female") { echo "女性"; } else { echo "未知性别"; } ?> |
代码的意思就是通过表单以POST方式提交性别选项,分别是男性和女性,然后交给php接收post上来的数据,并传给sex变量。最后进行判断,如果是male则输出男性,female输出女性,其它情况输出未知。
提交后
文中提到的if.php可以到这里测试
然后就是switch,这个也很好理解,类似于if和elseif的组合,只不过遇到很多种条件需要判断时,switch回把代码变得很简洁直观。
PHP代码
代码如下 | 复制代码 |
switch (变量名) { case 值1: code to be executed if expression = label1; break; case 值2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; } |
当变量中的值满足“值1”或者“值2”时,分别执行两个case下面的代码,然后千万记住每个“case”之间都要用“break;”分开。当变量中的值既不是“值1”也不是“值2”时就执行“default”下面的代码。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools