<?php
#echo "hello world!"; #号和//都是注释单行
echo "<hr>";
echo "<a href='http://www.baidu.com'>百度</a>";
echo "hi";
echo "<br><br><br>";
//1.定义变量, 跟js一样,也属于弱类型语言
$a = 10;
$b = "5";
$c = "8";
echo $a+$b;//php里的"+"仅仅是运算符,字符串拼接用".";
echo $b.$c."同城";
echo "<hr>";
var_dump($b);//输入变量的类型和值;
//2.变量的赋值,引用赋值
echo "<hr>";
$str1 = "hello";
$str2 = &$str1; //&把$str1在内存中所占的地址,复制给$str2;
$str2 = "word";
echo $str1;
//3.变量的变量
echo "<hr>";
$str3 = "hello";
$$str3 = "world";
echo $str3;
echo "<hr>";
echo $hello;
//4.超全局变量
echo "<hr>";
print_r($_SERVER);//输出数组
echo $_SERVER['SERVER_NAME']; //主机名
echo "<hr>";
echo $_SERVER['HTTP_USER_AGENT']; //UA 检测浏览器版本 window.navigator.userAgent.indexOf("浏览器名")
//5.常量 常量名总是大写
echo "<hr>";
define("PI",3.1415926+2);
echo PI*2;
//内置常量
echo "<hr>";
echo PHP_VERSION;//版本号
//魔术常量
echo "<hr>";
echo __LINE__;
//6.字符串,单引号,双引号
echo "<hr>";
$str4 = "hello world!";
echo "我要输出{$str4}world"; //双引号,直接识别成变量
echo "<hr>";
echo '我要输出$str4'; //单引号,直接识别成字符串
echo "<hr>";
echo "我要'输出$str4'";//双引号中套单引号,正常输出带单引号的变量值
echo "<hr>";
echo '我要"输出$str4"';//输出字符串
//7.定界符
echo "<hr>";
//END前边和后边都不能有空格,END是自己定义的,但要前后一致
$str5 = <<<END
woshihaoren
hehe
nizaikaiwanxiao
END;
echo $str5;
echo "<hr>";
$str6 = <<<HTML
<div>
<a href="http://www.baidu.com">百度</a>
</div>
<ul>
<li><a href="http://www.baidu.com">百度</a></li>
</ul>
HTML;
echo $str6;
//8.字符串转义
echo "<hr>";
$str7 = "adgdkljg\ngsfgkhgk";
echo $str7;
echo "<hr>";
echo nl2br($str7);
echo "<pre>我是换行的转义\n字符</pre>";//pre元素可定义预格式化的文本,被包围在pre元素中的文本通常会保留空格和换行符.
//9.数据类型转化
echo "<hr>";
echo "10" + true + "3hh" + false + "2aa" + "1ab2"; //17
echo "<hr>";
echo "100 hello" + 200; //300
//10.类型相关函数
echo "<hr>";
$str8 ="22222";
echo gettype($str8);//返回类型
echo "<hr>";
echo is_int($str8);//判断是否是数字,是的话输出1
var_dump($str8);//既输出类型,也输出值
//11.数组
echo "<hr>";
$arr1 = ["a","b","c"];
print_r($arr1);
echo "<hr>";
$arr2 = array("a","b","c");//跟js一样的传统数组, 1.索引数组
$arr2[3] = "d";//
$arr2[] = "e";//[]向数组尾部追加;
$arr2[1] = "f";//把数组下标为1的值替换
echo count($arr2);//获取数组的长度
echo "<hr>";
print_r($arr2);
echo "<hr>";
for($i = 0; $i < count($arr2); $i++){
echo $arr2[$i]."<br>";
}
//11.2 关联数组
$arr3 = array("username" => "zhangsan", "password" => "123456", "phone" => "13548962357");
echo $arr3["password"];
// echo $arr3[0];//不能找到下标,不能用for循环
// echo "<hr>";
// for($i = 0; $i < count($arr3); $i++){
// echo $arr3[$i];
// }
echo "<br>";
foreach($arr3 as $key => $value){
echo $key.":".$value."<hr>";
}
?>