0
点赞
收藏
分享

微信扫一扫

php-基础知识(1)

西曲风 2023-02-22 阅读 81



1.因为是直接在php页面学习练习的,所以代码比较多!看代码就行了


 2.echo, 变量


#01.输出
echo "Hello Word";

#02.我是注释
//我是注释
/*
我是注释
*/

#03.变量,大小写敏感,区分大小写
$color="red";
echo "My car is " . $color . "<br>";

#04.变量,对变量名称大小写敏感
$x=5;
$y=6;
$z=$x+$y;

#变量会在首次赋值时,被创建
$txt="Hello World";
$x=6;
$y=10.5;
echo $x+$y;


3.作用域:


(1)函数之外声明的变量拥有全局作用域,只能在函数以外进行访问
(2)函数内部声明的变量拥有局部作用域,只能在函数内部进行访问




#05.作用域:loacl:局部 global:全局 static:静态
#函数之外声明的变量拥有全局作用域,只能在函数以外进行访问
#函数内部声明的变量拥有局部作用域,只能在函数内部进行访问

$t1=10;

function test(){
$t2=20;
echo "t1=$t1" . "<br>";
echo "t2=$t2" . "<br>";
echo "t1+t2=$t1+$t2" . "<br>";
}
echo "t1=$t1" . "<br>";
echo "t2=$t2" . "<br>";
echo "t1+t2=$t1+$t2" . "<br>";
test();

#06.global关键字 :用于访问函数内的全局变量
//06.1
function test1(){

global $t1;
echo "test1 t1=$t1" . "<br>";
}
test1();

//06.2
function test2($t){

echo "test2 t1=$t" . "<br>";
}
test2($t1);
//06.3
function test3(){
$GLOBALS['x']=$GLOBALS['y']+$GLOBALS['t1'];
}
test3();
echo "test3 x=$x" . "<br>";

#07.static 关键字:当函数执行完毕后,会删除所有变量,不过当我们不要删除的时候
#可以使用static关键字,这个变量存储的是每当函数执行完毕后的值

function test4(){
static $t3=0;
echo $t3 .'<br>';
$t3++;
}
test4();
test4();
test4();


4.两种基本的输出方法


#08.两种基本的输出方法:echo 和 print
#echo 能够输出一个以上的字符串,比print快,不返回任何值
#print 只能输出一个字符串
#echo是一个语言结构,有无括号均可使用:echo或echo()
#print也是语言结构,有无括号均可使用:print或者print()
#08.1
echo "<h1>我是h1</h1>" . "<br>";
echo "我是 php" . "<br>";
echo "this " ,"is ","a ","desk" . "<br>";
#08.2
$t4="Learn PHP";
$t5="LABELNET";
$cars=array("yuan","ming","zhuo");

echo $t4 . "<br>";
echo "Study PHP at $t5" . "<br>";
echo "my name is {$cars[0]}";
#08.3
print "<h2>PHP is FUN</h2>";
print "Hello world <br>";
print "hi php<br>";
print $t4 . "<br>";
print "<p>$t5</p>";
print "my name is {$cars[1]}";




<pre style="font-family: Consolas; font-size: 10pt; background: white;">



举报

相关推荐

0 条评论