小胖学PHP总结2-PHP的b变量和赋值
一、总述
虽然PHP是弱类型语言,但是有时仍然需要用到类型转换。下面写一下类型转换和定义变量以及变量赋值。
1.1 类型转换
PHP中的类型转换和C语言一样,非常简单,只需在变量前加上用括号括起来的类型名称即可。
<?php //类型转换 说明:使用操作符转换变量的时候,不会改变原有的值,而使用settype函数转换时,改变了原有的值$num = '3.1415926r';echo '使用(integer)操作符转换变量$num类型:'. (integer)$num;echo '<p>';echo '输出变量$num的值:'. $num;echo '<p>';echo '使用settype函数转换变量$num类型结果:'. settype($num,'integer');echo '</p><p>';echo '输出变量$num的值:'. $num;echo '</p><p>';?></p>
PHP中有一些函数可以用来检测变量是否为指定的类型,比如is_bool()则是检测是否为布尔类型,is_string()检测是否为字符串类型等等。
1.2 定义常量
常量可以理解为值不变的量,常量值被定义后,在脚本的其他任何地方都不能改变,语法为:define(constant_name,value,case_sensitive),三个参数分别为常量名称(必选),常量的值(必选),是否大小写敏感(可选)。常量获取的方法有两种:第一种就是直接使用变量名获取;第二种就是通过constant()函数获取。要判断一个常量是否已经定义,可以使用defined(stringName)函数,成功返回true,否则返回false。
<?php //定义常量:define(),获取常量的值:constant(),判断常量是否被定义:defined()define('Message','常量的值');echo Message. '<br>';define('count1','常量的值2');echo count1;$name = 'count1';echo constant($name). '<br>'; //其实输出的count的这个常量echo defined('Message'). '<br>';?>
<?php //预定义常量echo '当前文件路径:'. __FILE__. '<br>';echo '当前行数:'. __LINE__. '<br>'; //82echo '当前PHP版本信息:'. PHP_VERSION. '<br>';echo '当前操作系统:'. PHP_OS;echo '<p>';?></p>
1.3定义变量以及变量赋值
和很多语言不同,在PHP中使用变量之前是不需要声明变量(PHP4.0之前是需要声明变量),只需要为变量赋值即可。
<?php //变量的赋值//第一种:变量直接赋值,例如$E='ss';//第二种:变量之间的赋值,变量之间的赋值是指赋值后两个变量使用各自的内存,顾不干扰;//第三种:引用赋值,引用的概念就是,当改变其中一个变量的值时,另一个也跟着发生改变,使用&符号表示引用。//变量之间的赋值$string1 = 'spcn';$string2 = $string1;$string1 = 'zhuangjia';echo '变量string2的值为:'.$string2.'<br>';echo '变量string1的值为:'.$string1.'<br>';echo '<p>';//引用赋值$i = 'spcn';$j = & $i;$i = "hello,$i";echo 'j的值为:'. $j.'<br>';echo 'i的值为:'. $i.'<br>';?></p>
<?php //全局变量可以在程序中的任何地方访问,但是在用户自定义的函数中是不可用的。如果想用的话,使用global关键字声明。$zy = '不会看到';$zyy = '会看到';function lxt(){// echo $zy .'<br>'; global $zyy; echo $zyy.'<br>';}lxt();?><?php //可变变量 $change_name = 'trans'; $trans = 'you are met'; echo $change_name.'<br>'; echo $$change_name; //实现原理类似转义字符,$change_name 代表了trans,然后再加上一个$符号,即输出的是$trans echo '<p>';?></p>
1.4 运算符
运算符和别的语言一样,包括+ ? * / () | ^ ~
<?php //@运算符:对错误信息进行屏蔽$err = @(5/0);echo $err.'<br>';?><?php //三元运算符$value = 100;$res = ($value == 100)?'三元运算':'没有改值';echo '哈哈'.$res.'<br>';echo '<p>';?></p>
1.5函数
函数,就是将一些重复使用到的功能写在一个独立的代码快中,在需要的时候单独调用,创建函数语法:function fun_name($str1,$str2....$strn){},然后调用的时候fun_name(XXX)。
<?php //简单函数function countNumber($num1,$num2){ return "$num1 * $num2 = ".$num1 * $num2.'<br>';}echo countNumber(10,10);echo '<p>';//函数之间传递参数//按值传递方式function example($m){ $m = $m * 5 + 10; echo '在函数内$m的值为:'.$m.'<br>';}$mm = 1;example($mm);echo '函数外$m的值为:'.$mm.'<br>';echo '</p><p>';//按引用传递方式function example1(&$m){ $m = $m * 5 + 10; echo '在函数内$mmm的值为:'.$m.'<br>';}$mmm = 1;example1($mmm);echo '函数外$mmm的值为:'.$mmm.'<br>';echo '</p><p>';//可选参数,其中$tax就是可选参数,填也行,不填也行function values($price,$tax=""){ $price += $tax; echo '价格为:'.$price.'<br>';}values(100,20);values(100);echo '</p><p>';//对函数本身引用 function &rExample2($tmp=0){ $tmp = $tmp.'123456'; return $tmp; }$str5 = &example2("kankan");echo $str5.'</p><p>';?></p>
版权声明:本文为博主原创文章,未经博主允许不得转载。

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

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.

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 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.

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 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.

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 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.
