Home Backend Development PHP Tutorial PHP基础知识(1)

PHP基础知识(1)

Jun 13, 2016 pm 12:03 PM
array function object php string

PHP基础知识(一)

? ? ? 1 标签 ,将PHP语句包含其中。

? ? ? 2 单行注释://

? ? ? 3 多行注释:/* ? */

? ? ? 4 变量名以$起始

<?php    $mycounter = 1;    $mystring = "Hello";    $myarray = array("One", "Two", "Three");?>
Copy after login

? ? ? ?

? ? ? 5 定义数组,array( );

<?php     $myarray = array('hi', 'hello');    echo $myarray[0];?>
Copy after login

?

? ? ? 6 字符串拼接(.)

<?php    echo "You have" . $msgs . " messages.";?>
Copy after login

?

? ? ? 7 PHP中字符串可以用单引号也可以用双引号,其区别在于PHP不会对单引号中的变量进行替换;而会对双引号中的变量替换成变量的实际值。

?

? ? ? 8 PHP中字符串可以跨多行;跨多行时还可以使用<<<操作符,将多行文本包含其中。

? ? ? 9 PHP是弱类型语言。意味着变量使用前不必先声明,且PHP可以根据上下文进行类型转换。

? ? ? 10 定义常量,define

define("ROOT_URL", "/hotdocs/");
Copy after login

?

? ? ? 11 变量作用域范围

? ? ? ? (1)局部变量,在函数中定义,且只能在函数中读取;

? ? ? ? (2)在函数外部声明的变量,只能由非函数代码读取;

? ? ? ? ? 注意:函数内正常情况下不能读取函数外部声明的变量,除非该变量的声明使用了global。

?

? ? ? 12 静态变量,只声明(初始化)一次。

? ? ? ? ? 注意:静态变量的赋值不能是表达式。

function test(){    static $count = 0;    echo $count;    $count++;}
Copy after login

?

? ? ? 13 PHP中的FALSE值输出为空,TRUE值输出为1。

? ? ? 14 强制类型转换(int),(bool),(float),(string),(array),(object)

$c = (int) ($a / $b);
Copy after login

?

? ? ? 15 传引用

function fix_names(&$n1, &$n2, &$n3){   ……}fix_names($a1, $a2, $a3);
Copy after login

?

? ? ? 16 引入文件

include                      //可能会重复引入include_once            //只引入一次//如果引入的文件未找到,程序继续执行
Copy after login

?

requirerequire_once//如果引入文件未找到,如程序不再执行
Copy after login

?

? ? ? 17 克隆对象,如果使用对象直接赋值(=),它们将指向同一对象引用。而通过clone方式可以产生不同的对象引用。

$object1 = new User();$object2 = $object1;/*****************/$object1 = new User();$object2 = clone $object1;
Copy after login

?

? ? ? 18 构造函数

class User {    function __construct($param1, $param2){        public $username = "Guest";    }}
Copy after login

?

? ? ? 19 析构函数

class User {    function __destruct(){        //    }}
Copy after login

?

? ? ? 20 静态方法,直接通过类名加上双冒号(::)调用,静态方法不能通过$this->property的方式读取类中的属性。

User::pwd_string()class User{    static function pwd_string(){        echo "Please enter your password";    }}
Copy after login

?

? ? ? 21 声明类的属性,类的属性声明时不能赋值为表达式或函数调用。

? ? ? 22 在类中声明常量,使用const关键字声明,在静态方法中可以通过self::property的方式获取所声明的常量。

? ? ? 23 静态属性和方法

? ? ? ? ? ?静态属性不能通过类的实例直接读取,但可以通过静态方法或实例方法获取。

? ? ? 24 继承 extends

? ? ? 25 在子类中调用父类的方法 parent::method();

? ? ? 26 final 方法,防止子类覆盖父类的方法。

? ? ? 27 向数组中添加元素

$paper[] = "Copier";$paper[] = "Inkjet";
Copy after login

? ? ? 28 关联数组

? ? ? 29 foreach ... as 循环

foreach($paper as $item){}
Copy after login

?

foreach($paper as $item => $description){}
Copy after login

?

? ? ? PHP函数示例

echo strrev(" .dlrow olleH");  //反转字符串echo str_repeat("Hip ", 2);    //重复字符串echo strtoupper("hooray!");    //大写strtolower("string");          //小写ucfirst("string");             //首字母大写function_exists();             //检查函数是否存在is_array($array);              //判断是否为数组count($array);                //返回数组元素个数sort($array);                  //数组排序sort($array, SORT_NUMERIC);sort($array, SORT_STRING);rsort($array);                 //数组反排序shuffle($array);               //随机顺序explode(delimiter, string);    //将字符串分隔成数组extract($_GET);                //将关联数组转成PHP中的变量compact(variableName);         //将多个变量转成关联数组reset($array);                 //让数组指向第一个元素end($array);                   //让数组指向最后一个元素printf();                      //格式化输出time();                        //返回当前时间戳mktime();                      //根据指定时间创建时间戳date($format, $timestamp)      //格式化输出时间信息checkdate();                   //时间信息验证
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles