Home Backend Development PHP Tutorial 【PHP】PHP入门第一章

【PHP】PHP入门第一章

Jun 23, 2016 pm 01:47 PM

一,PHP大小写敏感

        1)所有用户定义的函数、类和关键字都对大小写不敏感。

如下结果输出一致:

                 echo  "hello world"

 Echo  "hello world"

 EcHo  "hello world"

        2)用户自定义的变量,区分大小写

         如下只有第一行输出正确的 color

<?php $color="red";echo "My car is " . $color . "<br>";echo "My house is " . $COLOR . "<br>";echo "My boat is " . $coLOR . "<br>";?&gt;
Copy after login
二,全局变量 global关键词

       1)在函数内部的变量前面使用global关键词,访问全局变量。

<?php $x=5;$y=10;function myTest() {  global $x,$y;  $y=$x+$y;}myTest();echo $y; // 输出 15?>
Copy after login
        2)PHP中名为 $GLOBAL[index]的数组中存储了所有的全局变量,下标存储了所有的全局变量,下标存有变量名,这个数组在函数内也可以访问,并能够用于直接更新全局变量。

        

<?php $x=5;$y=10;function myTest() {  $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];} myTest();echo $y; // 输出 15?>
Copy after login
三,static关键词

       函数执行完成之后,局部变量都会被删除,有时候想让函数内部的变量有记忆性,采用static关键词。

        

<?phpfunction myTest() {  static $x=0;  echo $x;  $x++;}myTest();myTest();myTest();?>
Copy after login

四,基本输出方法

       echo 和 print 之间的差异:

               echo - 能够输出一个以上的字符串

               print - 只能输出一个字符串,并始终返回 1

       提示:echo 比 print 稍快,因为它不返回任何值。

<?php $txt1="Learn PHP";$txt2="W3School.com.cn";$cars=array("Volvo","BMW","SAAB");print $txt1;print "<br>";print "Study PHP at $txt2";print "My car is a {$cars[0]}";?&gt;
Copy after login


五,字符串 和 整数

      1)字符串是字符序列,比如 "Hello world!"。

           字符串可以是引号内的任何文本。您可以使用单引号或双引号:

      2)整数

           PHP var_dump() 会返回变量的数据类型和值
            

<?php $x = 5985;var_dump($x);echo "<br>"; $x = -345; // 负数var_dump($x);echo "<br>"; $x = 0x8C; // 十六进制数var_dump($x);echo "<br>";$x = 047; // 八进制数var_dump($x);?&gt;
Copy after login

结果:

int(5985) int(-345) int(140) int(39)
Copy after login


六,数组

$cars=array("Volvo","BMW","SAAB");var_dump($cars);
Copy after login


结果:

array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(4) "SAAB" }

 七, NULL值

         NULL标识变量无值,NULL是数据类型NULL唯一可能的值,NULL值表示变量是否为空,可以通过把值设置为NULL,将变量清空。

<?php $x="Hello world!";$x=null;var_dump($x);?>
Copy after login


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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles