Blogger Information
Blog 13
fans 0
comment 0
visits 6650
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php基础知识--2018年8月26日22时04分作业
七分钟的记忆的博客
Original
601 people have browsed it

什么是变量作用域?

1、全局作用域和局部作用域

在所有函数外部定义的变量,拥有全局作用域global;在函数内部定义的变量用于局部作用域local。全局变量可以被脚本的任何位置访问,但在函数内部访问全局变量要使用global关键字。而局部变量只能在函数内部进行访问。

2、函数内部访问全局变量

在函数内部访问全局变量,要使用global关键字

2、static作用域

当一个函数调用完后,它的所有变量都会被删除,如果想要某个局部变量不被删除,那么可以使用static关键字

实例

<meta charset="UTF-8">

<?php

echo "变量的数据类型的转换";

$age ="孙兴德";  //字符串
$salary =3500.25;  //float
$salary1 =25;  //integer
$salary2 =true;  //Boolean

echo "<hr>";
// 数组
echo "我叫".$age."工资".$salary."是否已婚".$salary2;
echo "<hr>";
echo "我叫",$age,"年龄是",$salary1,"工资",$salary,"是否已婚",$salary2;

echo "<hr>";

$books =['php','html','css','javascript','jquery'];

print_r($books);

// object
echo "<hr>";
$student = new stdClass();
$student ->name="孙兴德";
$student ->new1="php";
$student ->new2=80;

var_dump($student);
var_dump($student ->name,"<br>");
var_dump($student ->new2);
print_r($student ->new1);

// 资源类型 resource

// 打开文件
$file = fopen('test.txt','r') or die('打开失败');
// 读出来   读多少字节
echo fread($file,filesize('test.txt'));
//关闭
// fclose($file);

// 变量检测

echo gettype($file); // resource

// 设置类型
echo "<hr>";
$price =45.25;

echo gettype($price);
echo "<hr>";
settype($price,'integer');
echo $price;
echo gettype($price);

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
 *
 * @authors Marte (iqianduan@126.com)
 * @date    2018-08-23 17:00:43
 * @version $Id$
 */

// in_null()  检测变量是否存在
//
// empty()  检测变量是否为空
//
// isset()   检测变量是否定义

// $box1;
// $box2 =null;
// $box3 =0;
// @var_dump(is_null($box1) ? true : false);
// @var_dump(is_null($box2)? true : false);
// @var_dump(is_null($box3)? true : false);



// empty()  检测变量是否为空
// 空字符串、空数组、null、false、0、"0" 为false

// $box4 ="";
// $box5 =[];
// $box6 =0;
// var_dump(empty($box4) ?false : true);
// var_dump(empty($box5) ?false : true);




// isset()   检测变量是否定义


$box7 =null;
$box8 ="php";
var_dump(isset($box7)?false : true);
var_dump(isset($box8)?false : true);

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<meta charset="UTF-8">


<?php


 echo "<h1>流程控制</h1>";

// 分支语句
$age = 91;
if ($age < 60) {
    echo "不及格";
} else if ($age >= 60 && $age < 80) {
    echo "及格";
}else if ($age>80 && $age<=90) {
    echo "良";
}else {
    echo "优秀";
}

// 三元判断

$age2 = 18;
echo ($age2 < 18)? "未成年人禁止进入" : "可以上网";


echo "<hr>";
// switch


// strtolower转化成大写
$program = "Java";
switch(strtolower($program)){
    case 'php':
    echo "php是全世界最好的编程语言";
    break;
    case 'java':
        echo "java";
        break;
    case 'html':
    echo "html";
    break;
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<meta charset="UTF-8">


<?php
echo "for循环","<br>";

for ($i=0; $i<10 ; $i++) {
   // print($i . ",");


($i<9) ? print($i . ",") : print($i);

// print() 函数输出一个或多个字符串
//
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!