php学习笔记3—(变量、常量和操作符)

本文深入探讨了PHP中变量的使用方法及各种操作符的功能,包括算术、字符串、赋值、引用、比较、逻辑、位运算等操作符,并介绍了类型转换、常量定义、数据类型检查等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

$开头的是变量,变量的值还能当变量名,真好!这让我怀念起了LOGO ——我接触的第一门语言,语法跟汇编差不多。

<?php

$var_name='_next';
$_next='Variable: next';
echo $$var_name."<br>";   # $var_name的值是_next,所以这里$$var_name相当于$_next
echo  "$$var_name <br>";  # 这里$$代表单独一个$,就好像Makefile一样

# consts
define('E',2.718); # 定义常量
echo E.'<br>';            # 常量前边不加$

# 数据类型:integer, float, string, boolean, array, object
#
 类型转换
$str_E=(string)E;
echo $str_E.'<br>';

# operators
#
 number: +-*/% ++ -- += -= *= /= %=
#
 string: . .=
#
 assignment: = (跟C一样返回自己)
$str_conn="abc";
$str_conn.='def';
echo "$str_conn<br>";
# 赋值时先产生一个副本,然后再保存到目标去
#
 reference &     <-- not a pointer, just a alias
$ref=&$str_conn;
$str_conn='ghi';
echo "$ref<br>";
unset($ref);  # 解除引用
$ref="ref";
echo "$ref, $str_conn<br>";

# compare: ==/!= ===/!== <> < > <= >=
#
 == 值相等  ,  !=
$result=0=='0'?"true":"false"// true
echo "$result<br>";
# === 值相等,数据类型相同  ,  !==
$result=0==='0'?"true":"false"// false
echo "$result<br>";
# <> 不等
$result=0<>'0'?"true":"false"// false
echo "$result<br>";

# logical operators
#
 ! && || , and or 
#
 &&=and, ||=or, 不过and和or的优先级比较低,
#
 注意,or,xor,and,print的优先级比=还要低 
#
 =>print>and>xor>or>,

# bitwise operators
#
  & | ~ ^(xor) << >>

# other operators
#
 , ?:
#
 错误抑制操作符 @
$a=@(50/0);   // 不加的话将有除0警告,如果启用了php的track_errors属性,错误信息将会被保存在全局
              // 变量$php_errormsg中

echo $a;

# `` 执行shell命令
$a=`dir c:/`;
echo "<pre>";
echo "$a";
echo "</pre>";

# array operators
#
 + ==(相同元素) ===(元素相同,顺序相同) != <> !==

# instanceof
class sampleClass{};
$myObject=new sampleClass();
if ( $myObject instanceof sampleClass )
    
echo "myObject is an instance of sampleClass<br>";
else echo "myObject is not an instance of sampleClass<br>";

# gettype() settype()
$a=56;
echo gettype($a)."<br>";
settype($a,"float");
echo gettype($a)." ".$a."<br>";
# is_array()
#
 is_double(), is_float(), is_real  <--  they are the same
#
 is_long(), is_int(), is_integer() <--  they are the same
#
 is_string()
#
 is_object()
#
 is_resource()        resource和外部类型,null是一种特殊的类型,值是NULL
#
 is_null()
#
 is_scalar()     是否标量(integer, float, boolean, string)
#
 is_numberic()
#
 is_callable()   是否有效的函数名称

# isset() 
#
 empty()看是否为非空或者非0
if (isset($b)) {
    
echo "b is set<Br>";
else {
    
echo "b isn't set<br>";
}
if (isset($a)) {
    
echo "a is set<Br>";
else {
    
echo "a isn't set<br>";
}

# int intval()  // 类型转换
#
 float floatval()
#
 string strval()
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值