Video-03 No.2 PHP基本语法
?
?
<!-- modity by shma1664 --><?php /* * PHP标识号定义规则:与Java相同 * 变量的定义前面要加上一个" $ "符号标记 * PHP是弱类型语言,这个与JavaScript相识 * PHP支持如下的基本数据类型:Integer、Float、Double、String、Boolean、Array、Object * */ $sum = 10; echo $sum; $count = 2.5; echo $count; // 数据类型转换:隐式转换 $sum = $count; echo $sum; echo ("<br />"); /* * 数据类型转换:强制转换 * gettype():获取某个变量的类型,返回值是一个类型字符串 */ $sum = 25.6; echo gettype($sum); $count = (int) $sum; echo gettype($count); echo ($count); echo ("<br>"); echo ("<br>"); /* * settype(): 设置变量类型,返回值是一个boolean,是否设置成功 */ echo ("<br>"); $num1 = 100; echo settype($sum1, "string"); // boolean, integer, float, array, object, null echo ("<br>"); echo $num1; echo ("<br>"); echo ("----------------------------------"); echo ("<br>"); /* * isset(变量名, 变量名..): 判断某个变量是否存在 * unset(变量名, 变量名...): 销毁某个变量 */ $num2 = 12.58; $num3 = 12; echo isset($num2, $num3); echo isset($num3); unset($num2, $num3); echo isset($num2); echo isset($num1); echo isset($num1, $num3); echo ("<br>"); echo ("----------------------------------"); echo ("<br>"); /** * empty(变量名): 判断某个变量是否为空 * 若为空则返回1,非空则返回0 * null, 0, "", "0", false, array(), var $var以及没有任何属性对象都会被看成null */ $num4 = 12.4; $num5 = ""; $num6 = (boolean)0; $num7 = null; echo (empty($num4)); echo (empty($num5)); echo (empty($num6)); echo (empty($num7)); echo ("<br>"); echo ("----------------------------------"); echo ("<br>"); /** * 以上为判断是否属于变量是否属于某种类型 * 若是则返回ture,否则返回false */ echo is_double($num4); echo is_float($num4); echo is_int($num4); echo is_long($num4); echo is_null($num4); echo is_object($num4); echo is_array($num4); echo is_string($num4); echo ("<br>"); echo ("----------------------------------"); echo ("<br>"); /* * 临时转换变量 * intvar()、floatvar()、strvar() :临时转换变量类型为int、float、string */ $num8 = 10.1; echo gettype($num8); echo intval($num8); echo gettype(intval($num8)); echo floatval(intval($num8)); echo gettype(floatval(intval($num8))); echo strval($num8); echo gettype(strval($num8)); echo gettype($num8); echo ("<br>"); echo ("----------------------------------"); echo ("<br>"); //定义常量,常量定义后其值不能在发生改变 define("TOTAL", 23); echo TOTAL; //PHP预设常量,定义在phpinfo()函数里面 echo phpinfo(); echo ("<br>"); echo $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"]; ?>
?
?表单处理:
?
<!-- modity by shma1664 -->
<?php $username = $_POST['username']; $pwd = $_POST['pwd']; echo "您输入的用户名是:".$username; echo "<br />"; echo "您输入的密码是:".$pwd;?>?
<!-- modity by shma1664 --><?php /* * 双引号和单引号的区别: * 单引号会按照声明的原样解释,解释字符串时,变量和转移序列都不会进行解析 * <br /> : 在浏览器网页前台显示有效,后台源代码中显示无效 * 转义字符:在前台浏览器中显示无效。在后台源代码中显示有效 * 转义字符: * \n : 换行符 * \r : 回车符 * \t : 水平制表符 * \\ : 反斜杠 * \$ : 美元字符 * \" : 双引字符 */ $username = "shma"; $username2 = "马韶华"; echo "His name is $username"; echo '<br>'; echo 'His name is $username'; echo "<br>"; echo "他的名字是$username2,他已经23岁了!"; //无法显示 echo "<br>"; echo "他的名字是".$username2.",他已经23岁了!"; echo "<br>"; echo "他的名字是".$username2.",\n他已经23岁了!"; echo "<br>"; echo '他的名字是".$username2.",\n他已经23岁了!'; /* * == 与 === * != 与 !== * 恒等表示只有两个操作数相等并且类型相同时才相等,或者不等 */ $a = 6; $b = 5; $c = "5"; echo '<br>'; echo $a == $b; echo $a != $b; echo $a === $b; echo $a !== $b; echo $c === $b; echo $c == $b; // 错误抑制操作符 @ $num = @(10/0); echo "$num"; $value = 10; echo "value = ".($value > 1 ? $value : "0"); // 数学运算 /* * + : 数字之间运算 * . : 字符串之间运算 */ $a1 = 'a'; $b1 = 5 .$a1; echo $b1; // is_numeric — 检测变量是否为数字或数字字符串 $a = 123; if(is_numeric($a)) { echo $a."是数字"; } else { echo $a."不是数字"; } echo "<br>"; // 获取随机数 echo rand(); echo "<br>"; echo rand(1, 10); echo "<br>"; echo mt_rand(); echo "<br>"; echo mt_rand(1, 10000); echo "<br>"; echo getrandmax(); echo "<br>"; echo mt_getrandmax(); //格式化数据 $a = 10324.564343; echo "<br>"; echo "<br>"; echo number_format($a); echo number_format($a,2); echo number_format($a,2,"#", "!"); //数学运算 $b = -6.3; echo abs($b); echo min(1,3,4,5,6,-5); echo max(1,3,4,5,6,-5);?>?
?

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.

SublimeText3 Linux new version
SublimeText3 Linux latest version

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