Home > Backend Development > PHP Tutorial > [php learning 2] Basic grammar exercises 1

[php learning 2] Basic grammar exercises 1

WBOY
Release: 2016-07-28 08:27:57
Original
1211 people have browsed it

Since the results are almost filling one page, let’s record them first~!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>First Php</title>
</head>
<body>
<h1>三好少年</h1>
<?php
/*基本语法练习
*1,定义变量必须加$,但不用写类型.这就导致了定义和赋值是一样的语句了.
*/
$x = 5;
$y = 10;
function Test(){
    global $x , $y;/*访问全局变量必须加GLOBAL*/
    static $z = 0;/*静态局部变量*/
    $y = $y + $z;
    $z++;
}
Test();
Test();
echo $y,&#39;<br>';//11

/*
 * 1,echo没有返回值,可以输出多个字符串,逗号隔开~
 * 2,Print总返回1,只能输出一个字符串~
 * 3,单引号只能输出字符串,双引号能输出变量~
 * 4,var_dump这个犀利啊,能监测变量类型,并输出值.
 * 5,数组元素可以不同类型,这个语法也是没谁了.
*/
$cars = array("Volvo","BMW","TOYOTA",5);
echo "字符串1","字符串2",'字符串3';
echo '<h2>PHP IS Fun</h2>';//还能输出标签~
echo "$cars[0]<br>";
print("print只能一个字符串<br>");
print("print也能输出标签<h2>print标签</h2>");
var_dump($cars); print '<br>';

/*
 * 1,常量的定义真不怎么样,默认区分大小写,想不区分的话,最后参数为False;
 * 2,print(china_display + constInt + '<br>');这个有错误,但能运行,只显示800;看来这个编译器不是很严格.~
 * 3,常量默认全局的,函数调用的话不用Global修饰;
 * */
define("china_display","中华人民共和国");
define("constInt",800);
echo china_display,constInt,'<br>';
function TestConst($a){
    echo china_display, $a, '<br>';
}
TestConst("伟大");

/*
 * 1,两个字符串链接有个叫"并置运算符"用.标示,链接两个字符串.~
 * 2,strlen对于汉字的输出是n*3,不是n*2;
*/
$txt1 = '字符串第一部分';
$txt2 = '字符串第二部分';
print $txt1 . $txt2 .'<br>';
echo 'abc 长度',strlen("abc"),";","你好么 长度",strlen("你好么"), "<br>";
echo 'abc b的位置',strpos("abc","b"),";","你好么 好的位置",strpos("你好么","好") ,"<br>";

/*
 * 1,比较运算符中,==和===是两码事,==纯粹的比较,====加上了类型的判断.
 * 2,echo false什么也不输出;
 * 3,isset()如果变量未设置或者为NULL,则返回False,否则True;挺重要的函数啊~;
 * 4,empty()如果变量不存在或其值为""、0、"0"、NULL、、FALSE、array()、var $var; 以及没有任何属性的对象,则返回True,否则False;
 * */
$int100 = 100;
$str100 = '100';
function BoolToStr($A){
   if ($A === true){
       return "True";
   }
   else{
        return "False";
   }
}
echo BoolToStr($int100 == $str100) , ",", BoolToStr($int100 === $str100),"<br>";//true,false
$ussername = true?"0":"hello";
print BoolToStr(isset($ussername)) . '<br>';//True
print BoolToStr(empty($ussername)) . '<br>';//True

/*
 * 1,switch也支持字符串;
 * 2,没有Default也不抱错.
 * */
$favcolor="red";
switch ($favcolor) {
    case "red":
        echo "你喜欢的颜色是红色!";
        break;
    case "blue":
        echo "你喜欢的颜色是蓝色!";
        break;
    case "green":
        echo "你喜欢的颜色是绿色!";
        break;
    default:
        echo "你喜欢的颜色不是 红, 蓝, 或绿色!";
}

/*
 * 1,count能计算出数组长度,这中写法有点LOW,早晚写个全局类来替代它.
 * 2,Count对字符串无效,结果为1;
 * 3,关联数组的类型也是混着都可以.前面是索引,后面是值,所以这类数组只能Foreach遍历;
 * */
echo "Cars数量:",count($cars),'<br>';//4
echo count("aaaaddd"),'<br>';//1
for ($x = 0; $x < count($cars); $x ++) {
    echo $cars[$x], '<br>';
}
foreach($cars as $x){
    echo $x, '<br>';
}
$ages = array('梅西'=>29, "C罗"=>'31',5=>8);
foreach($ages as $x => $x_value){
    echo "name:",$x,"age:",$x_value,'<br>';
}

echo $ages["C罗"];
?>
</body>
</html>

<?php
?>
Copy after login

The above introduces [php learning 2] basic grammar exercise 1, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template