标签:style blog http io ar color os 使用 sp
一、变量的命名规则:<?php
$a = ‘abc‘;
$b = "xyz";
// 1,单引号内的变量不能被解析,双引号内的变量可以被解析
$c = ‘$a‘;
$c = "$a";
var_dump($c);
// 2,单引号内可以插入双引号,双引号内可以插入单引号
/*echo $d = ‘nihao "zhongguo"‘;
echo $e = "nihao ‘taiwan‘";
*/
//3,单引号可以解析 单引号,\ ,双音号可以解析更多的转义字符
echo $f = ‘ni\\h\n \$ao \‘hanguo\‘‘;
echo $h = "ni\nhao \"jiangsu\" \$";
//4,单引号的效率更高
//5,拼接两个变量,或者拼接变量和字符串使用 "."
echo ‘<br>‘;
$a = ‘wo ai ni zhongguo,‘;
$b = ‘wo ai ni beijing‘;
echo ‘我说:‘.$a.$b.‘zheng doushi zhende?‘;
$color = ‘red‘;
$border = ‘10‘;
//6单引号内插入双引号,双引号内的变量不会被解析
echo ‘<table border="‘.$border.‘" bgcolor="‘.$color.‘"><tr><td>内容</td></tr></table>‘;
$border = 2;
$color = ‘pink‘;
//7双引号内插入单引号,单引号内的变量会被解析,同时保留单引号
echo "<table border=‘$border‘ bgcolor=‘$color‘><tr><td>内容</td></tr></table>";
// 用途2: 不需要记下 了解即可:$username = ‘jack‘;
// $sql = "select * from bbs_user where user=‘$username‘";
<?php
header("content-type:text/html;charset=utf-8");
//布尔值参与算术运算时 true 转为1 false 0
$boo = true;
$boo = false;
var_dump($boo + 10);
//下列情况为false
//1、整型0
$num = 0;
$num = -0;
//2、浮点型
$num = 0.0;
$num = 1.1;
//3、字符串
$num = ‘a‘;
$num = ‘abc‘;
$num = ‘0.0‘;
$num = ‘false‘;
$num = ‘0‘;
//4、空字符串
$num = ‘‘;
//5、空数组、空对象
$num = array();
$num = array(0);
//6、null、空资源
$num = null;
//7、false本身
//8、被销毁的变量unset();
if($num){
echo‘真‘;
}else{
echo‘假‘;
}
unset($num);
var_dump($num);
<?php
//如果非数字开头的字符串参与算术运算,字符串转成0
$str = ‘alibbaba‘;
//如果字符串以数字开头,参与算术运算,只有数字部分会参与运算
$str = ‘10abc‘;
$str = ‘10.01abn‘;
//$str = ‘abc100‘;
var_dump($str + 10);
结果:float(20.01)
<?php
$f = 10.1;
// 任何一个浮点参与算术运算,结果必然是浮点
var_dump(10.0 + 10);
var_dump(‘10.0abc‘ * 10)
结果:float(20) float(20)
<?php
define("CON_INT",100);
//输出:100
echo CON_INT;
define("GREETING","HELLO you",true);
//输出:HELLO you
echo GREETING;
//输出:HELLO you
echo constant ("Greeting");
<?php
const DB_HOST = ‘localhost‘;
define(‘DB_USER‘,‘root‘);
echo DB_HOST;
echo DB_USER;
// 返回当前文件的完整路径和文件名
echo __FILE__;
//返回当前所在的行数
echo __LINE__;
function show()
{
// 当前函数名
echo __FUNCTION__;
echo ‘aaaa‘;
}
// 调用函数方式:函数名+()
show();
/*
// 返回当前类名
__CLASS__;
//返回当前的方法名
__METHOD__;
*
<?php
$db_host = ‘127.0.0.1‘;
$db_user = ‘lily‘;
$db_pwd = ‘123456‘;
$str=<<<ETO
<?php
//开启会话
session_start();
//设置网页字符集
header("content-type:text/html;charset=utf-8");
//设置时区
date_default_timezone_set(‘PRC‘);
//数据库相关设置
const DB_HOST = ‘$db_host‘;
const DB_USER = ‘$db_user‘;
const DB_PWD = ‘$db_pwd‘;
const DB_NAME = ‘bbs81‘;
const DB_CHARSET = ‘utf8‘;
// 链接数据库
$link = mysql_connect(DB_HOST,DB_USER,DB_PWD)or die(‘连接数据库失败‘);
mysql_select_db(DB_NAME);
mysql_set_charset(DB_CHARSET);
ETO;
// 将字符串写入指定文件,如果文件不存在自动创建
file_put_contents(‘config.php‘,$str);
<?php
$str = ‘abc‘;
$str = 123;
$str = array();
echo gettype($str);
echo‘<hr>‘;
//强制将字符串转换为整型
$str = ‘11xx2344zz‘;
$result =(int) $str;
echo gettype($str);
echo ‘<br>‘;
//var_dump($result);
echo gettype($result);
echo‘<hr>‘;
$arr = array(1,2);
$result= (int) $arr;
echo gettype($arr);
echo ‘<br>‘;
echo gettype($result);
echo‘<hr>‘;
//强制将字符串转换为数组
$str =‘a,b,c‘;
$result = (int) $str;
echo gettype($str);
echo ‘<br>‘;
echo gettype($result);
echo ‘<br>‘;
echo $result;
echo ‘<hr>‘;
//强制转换为字符串
$str = 1;
$result = (string) $str;
echo gettype($str);
echo ‘<br>‘;
echo gettype($result);
echo ‘<br>‘;
echo $result;
echo ‘<br>‘;
var_dump($result);
echo ‘<hr>‘;
//获取数据的类型
$num = 10;
echo gettype($num);
echo ‘<hr>‘;
//获取变量的整数值(为啥结果中不显示呢?)
$result = intval($num);
var_dump($result);
echo ‘<hr>‘;
//获取变量的字符串值
var_dump(strval(123456));
<?php
header("content-type:text/html;charset=utf-8");
$arr = array(‘username‘=>‘jack‘,‘sex‘=>‘男‘,‘age‘=>23);
// $arr = ‘abc‘;
/*echo ‘<pre>‘;
print_r($arr);
var_dump($arr);*/
$arr = ‘新中国成立啦‘;
if(is_array($arr)){
echo ‘<pre>‘;
print_r($arr);
}else{
echo ‘<pre>‘;
var_dump($arr);
}
// 判断是否是一个有效的函数
if(is_callable(‘xx‘)){
echo ‘这是一个有效的函数‘;
}else{
echo ‘这不是一个函数‘;
}
function xx()
{
echo ‘xxx‘;
}
$str = ‘1000abc‘;
//将字符串转换成整型
settype($str,‘int‘);
var_dump($str);
标签:style blog http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/lxw89/p/2ab3a6815dcacc3d7ce18f8bf0db1427.html