search
HomeBackend DevelopmentPHP Tutorial Video-03 No.2 PHP根本语法

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);
?>
?

?

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment