Detailed explanation of PHP variable definition and scope code examples

黄舟
Release: 2023-03-06 15:30:02
Original
1375 people have browsed it

1.                                                                                 php When calling attribute in a method, you cannot use echo directly. In the class, you must use $thisobject

1, PHP variable Definition and use: Letters starting with an underscore ^[a-zA-Z_]\w*2, Scope of PHP variable:                        Local scope: in FunctionVariables defined within the function can only work inside the function

                                                                                ‐                                                 ’ down to ‐ ‐ ‐ ‐ ‐ ‐ global scope: global $var

  $GOOBALS[‘var’]
Copy after login

                                                                                           Pass by reference): 1. Pass by value: # 4.

Super global variables

(predefined variables): ① $GLOBALS Save global variables

② $_SERVER Save

Service

Information③ $_GET Save get data

④ $_POST Save post data

⑤ $_

FILE

S Save submitted file⑥ $_

COOKIE

Save the obtained cookie

⑦ $_SESSION Save the session data⑧ $_REQUEST Save the request data

⑨ $ _ENV Save environment

2.            

Constant

##define() function can define constants

defined () Determine whether a constant is defined

Features:

(1) There is no need to use $

to define a constant (2) Constants do not care about the scope, they can be Use

anywhere (3) Once a constant is defined, it cannot be copied again

Magic constant(7):

FILE Get the absolute path and file name of the current file

LINE The current line number of the file

3. Control structure

## 1,

return

;

(1) Use return in the function to immediately stop the execution of the function and return the value of the function; (2) In the php script Used in, stop the execution of script file code

2, require_once()

has the same effect as require(), both introduce external files, the difference is require_once() , if the file has already been included, it will not be included again to avoid function redefinition and variable reassignment

3. The difference between include() and require() is that include() )RepeatWhen including files, a warning level error is generated; require() generates a fatal error

4. Passing of function

parameters, default It is also a value transfer. Even if the value of the parameter is modified inside the function, it will not affect the value outside the function.

If you want to affect the transfer by reference, the reference is the value saved by this variable in the memory space

5. PHP’s

data type

1. Scalar type

字符串

1,字符串的定义

单引号:单引号会将内容原封不动的输出---执行速度快

双引号 (比较):双引号会解析变量---执行速度慢

2,常用的字符串函数(重点)

查找:

//strpos(haystack,needle);查找字符串首次出现的位置
$str3 ='you are beautiful!';
                      $a= 'a';
   $b= strpos($str3,$a);//字符串首次出现的位置
                 //strrchr() 从字符串最后一次出现的位置开始,返回子字符串
Copy after login


替换:

   //trim()去掉字符串首尾空白字
                   echo trim($str4);//去除前后空格
//str_replace(查找的字符串,替换的字符串,在哪个字符串中查找);
$str5 = FILE;
   $str6= str_replace('string.php','',$str5);
//substr_replace()把字符串的一部分替换成另一个字符串
echo substr_replace($str8,'mathbook',11);//替换
//substr(string,start,length);返回字符串的子串
echo substr($str8,8);//从m开始截取
Copy after login

比较:

        //strcasecmp()以不区分大小写的方式比较字符串,返回0表示字符串相等
//将字符串全部转化为小写 strtolower($str1);
//将字符串全部转化为大写 strtoupper($str2);
Copy after login

拆分:

    //explode将字符串根据某个定界符分割成一个数组
                  $str_1 ='php,3g,.net,java';
                   $str_2= explode(',',$str_1);//根据某个定界符,将字符串分割成一个数组
                   var_dump($str_2);
//implode()通过一个定界符使数组元素连接成一个字符串
                  $arr =array('php','3g','.net','java');
                   echo$str_3 = implode($arr,',');//根据定界符,将数组连接起来
Copy after login

3,正则表达式匹配:

   preg_match()  进行正则表达式匹配
                  //php支持正则表达式,正则可以比喻成一个筛子,js可以使用它,php也可以使用
                  echopreg_match('/(HTTP:\/\/W{3}\.)(.+)/i','HTTP://WWW.sina.com',$match);
                   //如果提供了第三个参数,会将整个正则表达式匹配的所有结果放到数组中
      echo $domain = $match[2];
preg_replace(正则表达式,替换的结果,查找的字符串) 
 //将sina.com替换成360.com
    echo preg_replace('/sina\.com/','360.com','HTTP://WWW.sina.com');
//按照正则表达式匹配的结果进行替换
Copy after login

整型

浮点型

布尔型

2,复合类型

数组:

1, 数组的定义 array() 或 []

2,遍历数组(重点)

(1)使用for()遍历数组,适合下标从0开始,连续的整型索引数组

             $arr =array('php','3g');
                      //count()统计数组元素的个数也就是数组的长度
                      for($i=0;$i<count($arr);$i++){
                     echo$arr[$i];
   }
(2)foreach()遍历数组
$array =array(&#39;php&#39;,&#39;.net&#39;,&#39;subject&#39;=>&#39;java&#39;);
    foreach($arrayas $key => $value){
   echo$key.&#39;:&#39;.$value;
   echo&#39;<br/>&#39;;
   }
Copy after login

3,数组指针

   (1)current()获得当前数组指针指向的数组元素

(2)next()  将数组的指针指向下一个

(3)prev()  将数组的指针向前移动一个

(4)reset() 重置数组指针

(5)end()   将数组指针移动到最后

4,常用的数组函数

                array_push() 入栈,讲一个或多个元素压入数组的末尾

array_pop()   出栈,将数组最后一个元素弹出

array_shift()  将数组开头的元素移出

array_unshift()  在数组开头插入一个元素

 

              对象

       3,特殊类型

       NULL

       Resource

The above is the detailed content of Detailed explanation of PHP variable definition and scope code examples. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!