初学php——基础语法

<?php
//变量
//$4years ='long';
$_4years = 'long';

$name="Tom";
$Name="Jerry";
/*
echo "========$name,$Name";
echo $name;
echo $Name;
*/
unset($name);
echo "$name,$Name";

echo "<hr>";
//可变变量
$var="name";
$name="Tom";
echo $var;
echo $$var;

echo "<hr>";
//字符串
$coffee='Nescafe';
echo '$coffee taste is nice.<br>';
echo "$coffee taste is nice.<br>";
echo "I drank some $coffees.<br>";
echo "$coffee taste\" is nice.";

echo "<hr>";
//整型
echo $decad_num=1234; echo "<br>";//十进制
echo $octad_num=02322;echo "<br>";//八进制
echo $hexad_num=0x4D2;echo "<br>";//十六进制
//浮点型
echo $real_num=0.1234;echo "<br>";
echo $real_num=2.17e3;echo "<br>"; //2.17*10e3
echo $real_num=31E-2; echo "<br>";//31*10e-2
echo $real_num=2.0E+308;echo "<br>";
echo $real_num=1.8E+308*0;
echo "<hr>";
//数组,接受任意数量用逗号分隔的 键(key) => 值(value)
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // bar
echo $arr[12];    // 1
echo "<br>";
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo $arr["somearray"][6];    // 5
echo $arr["somearray"][13];   // 9
echo $arr["somearray"]["a"];  // 42
echo "<br>";
// 这个数组与下面的数组相同 ...
$arrs1 = array(5 => 43, 32, 56, "b" => 12);

// ...
$arrs2 =array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
echo $arrs1[5];
echo $arrs1[6];
echo $arrs1[7];
echo $arrs1["b"];echo "<br>";
echo $arrs2[5];
echo $arrs2[6];
echo $arrs2[7];
echo $arrs2["b"];
echo "<br>";
$array = array(1, 2, 3, 4, 5);
print_r($array);
echo $array[2];
echo "<br>";
// 现在删除其中的所有元素,但保持数组本身不变:
foreach ($array as $i => $value) {
    unset($array[$i]);
}
print_r($array);

echo "<hr>";
//类型转换
var_dump(1.2);
echo gettype(3.14);
print_r($name);
echo "<br>";
//强制类型转换
echo $str='123abc';echo "<br>";
echo $int=(int)$str;echo "<br>";
$arr = settype($int,'string');//$array=array(0=>123);?????????????????????????????
print_r($arr);

echo "<hr>";
$foo = "123.4abc"; // string
echo $int=intval($foo);echo "<br>";
echo $int=floatval($foo);echo "<br>";
echo $int=strval($foo);
echo "<hr>";
echo $foo=sprintf("%0.2f",3.1415926);echo "<br>";
echo $foo=(int)sprintf("%0.2f",3.1415926);

echo "<hr>";
//PHP外部变量
//1.表单变量
echo "<form action=\"1.php\" method=\"post\">";
echo "<input type=\"text\" name=\"username\" value=\"\">";
echo "</form>";

//1.php
echo $_POST["username"];
//2.环境变量
//echo phpinfo();
echo $_SERVER['REMOTE_ADDR'];//客户端ip地址

echo "<hr>";
//表达式
//foreach循环,遍历数组时用到
/*
foreach(ArrayExpression as $value){
 Some statements;
}
或者
foreach(ArrayExpression as $index=>$value){
 Some statements;
}
*/
$arr=array('apple','orange','pear');
print_r($arr);
echo "<hr>";
$i=0;
foreach($arr as $fruit){
 echo "第".$i."种水果是:".$fruit."<br>";
 $i++;
}
echo "<hr>";
//或者
foreach($arr as $index => $fruit){
 echo "第".$index."种水果是:".$fruit."<br>";
}
//函数
echo "<hr>";

echo "自定义函数第一种情形:<br>";
function cal_circle_area($radius){
 $area = M_PI*($radius*$radius);
 return $area;
}
$the_radius=5;
$the_area=cal_circle_area($the_radius);
echo "半径为".$the_radius."的圆的面积是".$the_area;

echo "<br>自定义函数第二种情形:<br>";
echo "半径为5.0的圆的面积是";
echo get_circle_area(5.0);
function get_circle_area($radius){  //注意:定义函数名不能重复
 return M_PI*($radius*$radius);
}
echo "<br>";
if(function_exists("get_circle_area")){
 echo "========";
}

echo "<hr>";
//传值
$input_array=array(5,9);
function take_sum($input){
 echo "$input[0]*$input[1]=",$input[0]*$input[1];
}
take_sum($input_array);
echo "<hr>";
//传址
function change_data(&$string){
 //$string.='We changed something.';
 $string=$string.'We changed something.';
}
$str='This is original string.';
change_data($str);
echo $str;

echo "<br>";
//函数默认参数
function hello_teens($name,$age=10,$sex="M"){
 echo "$name is a";
 if($sex=="M")
  echo " boy. He is ";
 else
  echo " girl. She is ";
 echo "$age years old.\n";
}
hello_teens("Ailce",9,"F");echo "<br>";
hello_teens("Jack",8);echo "<br>";
hello_teens("Tom");

echo "<hr>";
//变量的作用范围
//局部变量
$var="Hello, PHP World!";
function TestVar(){
 echo $var;
}
TestVar();echo "+++++++++";
echo $var;echo "=========";
echo "<br>";
//全局变量形式一
$var = "Hello, World!";
function TestVar1(){
 global $var;
 echo $var;
}
TestVar1();
echo "<br>";
//全局变量形式二
$var = "Hello, My World!";
function TestVar2(){
 echo $GLOBALS["var"];
}
TestVar2();

echo "<hr>";
//静态变量
function StaticVar(){
 static $va = 0;
 $va = $va + 1;
 echo $va;
}
StaticVar();echo "<br>";
StaticVar();echo "<br>";
StaticVar();

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值