search

  1. php基本简介

    1. 为何要学习php

      通过上网查资料,了解了基本的php知识,并知道了php的优缺点。php是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。php 独特的语法混合了C、Java、Perl以及php自创的语法。它可以比CGI或者Perl更快速地执行动态网页。用php做出的动态页面与其他的编程语言相比,php是将程序嵌入到HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成html标记的CGI要高许多;php还可以执行编译后代码,编译可以达到加密和优化代码运行,使代码运行更快。

    2. php和html,配置php环境

  2. php语法支持html语法,可以在php的代码之中完美的嵌套标签语言。PHP是将程序嵌入到HTML文档中去执行,执行效率比完全生成htmL标记的CGI要高许多。
  3. 跟着网上教程学会了php环境的配置,并成功运行第一个php文件。
    1. 基础php:
      echo,print,strings,arithmetic,variables,semicolons,comments
  4. 了解echo和print的区别:echo可以输出多个值,每个值之间用逗号隔开,而print只能输出一个值。
  5. 在输出string类型时,echo和print均可,多个字符串连接在一起时可用英式点即”.”相连。
  6. arithmetic即在输出时支持算术运算,比如echo 2 * 3;,即将输出6。
  7. variables,php的变量没有类型而言,换句话说即时自动匹配类型,定义方式类似$name = “wsy”;,即每个变量名之前只需要加一个$符号,剩下为自己定义的名字即可。变量在被初次赋值后才被定义,所以不需要$name;如此定义变量。
  8. 每条php语句都需要以分号结尾,这点与C/C++一样
  9. comments,注释也与C/C++的注释一样,可是使用//,也可以使用/**/。
  10. php条件控制

    1. 比较符号

      php的比较符号类似C/C++,也分为大于(>),小于(=),小于等于(

    2. if和else和elseif

      php中的if语句与C/C++很类似,也在()内添加条件,同时也支持这样if(1)或者if(true)的简化写法。

    3. switch,endswitch

      php中也存在switch,写法还是与C/C++一样,

      switch ($a) {	case 1:		echo 1;		break;	default:		echo 2;}

      相比于普通的写法,php还提供了另外一种的写法,这种写法省去了花括号。

      switch ($a) :	case 1:		echo 1;		break;	default:		echo 2;endswitch;
  11. php数组

    1. 数组的定义

      因为php对于变量是自动匹配类型,则php的数组对于不同类型的变量也是一视同仁,即php的数组可以添加任何类型的变量在一个数组当中。
      $a = array(“Tom”, “Jim”, 1, 2);
      如此定义了一个名为a的数组,含有4个元素,2个string类型,2个数字类型。

    2. 数组元素的获取,[],{}

      通过数组下标(同C/C++也是从0开始)来访问数组的元素有2种方法,一个是用[],另一个是用{}。这2个方法是一样的,不管用哪种方法都可以访问数组元素。

      echo $a[2];  //输出1echo $a{1};  //输出Jim
    3. 数组元素的修改

      php支持对数组元素的修改,修改方法即通过[]或者{}访问对应的数组元素,然后将其赋予新的值即可。

      $a[1] = “TTT”;echo $a{1};  //输出TTT,因为已经修改了。
    4. 数组元素的删除

      php支持对数组中某个元素的删除或对整个数组的删除的功能。通过unset();即可删除对应的元素。

      unset($a[1]);  //删除了数组中的第二个元素,现在数组包含Tom,1,2unset($a);  //删除了整个数组
  12. php循环

    1. **`for`循环**

      php中的for循环与C/C++相似,支持++操作符,但不支持+=,所以对于不是+1的循环,需要写成$i = $i + 3;

      for ($i = 0; $i <= 100; $i = $i + 10) {	//……}
    2. **`foreach`循环**

      当需要循环整个数组时,写for循环可能会过于繁琐,所以php提供了一种特殊的循环方法,即foreach循环。写法如下:

      $a = array(“Tom”, “Jim”, 1, 2, 3);foreach ($a as $i) {	echo $i;}

    这样就可以将php数组中的所有元素都输出了。
    foreach (数组名 as 变量名),接下来对于数组中每个元素操作即对用户自己取变量名的变量操作即可,但不支持修改功能,类似与C/C++中的传值与传址的区别。

    1. **`while`循环,`endwhile`**

      php中的while循环有2种写法,普通的写法是都所知道的。

      while () {	//……}

      类似于之前的switch,php中也有另外一种写法。

      while ():	//……endwhile;

      同if/else一样,while同时也支持这样while(1)或者while(true)的简化写法。

    2. **`do-while`循环**

      php中的do-while循环与C/C++一样。

      do {	//……} while ();

      do-while也支持这样do-while(1)或者do-while(true)的简化写法。

    3. **循环嵌套**

      php中支持各种循环的相互嵌套。

  13. php函数(1)

    1. 关于字符串的相关函数

      strlen(string)substr(string, start, length)strtoupper(string)strtolower(string)strpos(string, string)

      strlen(string):此函数用于求传入形参字符串string 的长度,返回数字类型即字符串的长度。
      substr(string, start, length):此函数用于求字符串string的某个子串。第一个形参为原字符串string。第二个形参start为子串在string中的开始位置,为数字类型变量。第三个形参length为子串的长度。此函数根据传入的形参,返回字符串类型的子串。
      strtoupper(string):此函数用于将字符串string全部转换为大写。返回大写的string。
      strtolower(string):此函数用于将字符串string全部转换为小写。返回小写的string。
      strpos(string, string):此函数用于求在第一个string中第一次出现第二个string的首位置,若从未出现返回false。

      strpos("emily", "e");   // 0strpos("emily", "ily");  // 2strpos("emily", "zxc");  // false
    2. **关于数学的相关函数**
      round(number(, n))rand(min, max)

      round函数用于对浮点数保留小数。
      round(number):这样写,默认对number保留为整数。
      round(number, n):这样写,对number保留n位小数。
      rand(min, max):此函数用于求给定范围[min, max]中的随机数。

    3. **关于数组的相关函数** 
      array_push(array, number/string)count(array)sort(array)rsort(array)join(string,array)

      array_push(array, number/string):此函数用于对数组array尾端增加一个元素,可以为任意类型。
      count(array):此函数用于求数组array中元素的个数。
      sort(array):此函数用于将数组array中的元素按字典序从小到大排列。
      rsort(array):此函数用于将数组array中的元素按字典序从大到小排列。
      join(string, array):此函数用于将数组中的元素用string连接起来,并返回连接后的结果。

      $array = array(5, 3, 7 ,1);rsort($array);print join(":", $array);//输出 7:5:3:1
  14. **php函数(2)**
    1. **自定义函数**

      php中允许用户定义自己所需的不同的函数,但不需要写函数类型。

      function Template() {	//……}

      function 代表定义的是一个函数,Template为函数名,用户可以随意定义,不需要考虑函数的类型。

    2. **带形参的自定义函数**

      在函数定义时,()之中可以为空,也可以添加形参,即称为带有形参的函数。

      function aboutMe($name, $age) {	echo "Hello! My name is " . $name . ", and I am " . $age . " years old.";}

      此函数定义了两个形参,分别为$name和$age。调用时,aboutMe("wsy", 22);即可,也可以传入其他变量名。

    3. **自定义函数的返回值**

      php的函数虽然没有函数类型,但是可以返回任意类型的值。

      function returnName() {	return "wsy";}

      return后可以跟变量也可直接跟常量。

  15. **php对象(1)**
    1. **类的定义、对象的定义**

      同C++中类的定义相同,php的类定义如下。

      class Dog {		public $name;		public $numLegs = 4;		//可以定义公有成员,可以对其赋值也可以对其不赋值		//……}

      建立一个类的对象。
      $dog = new Dog();
      调用公有成员。

      echo $dog->numLegs;//name因为未赋值,若要输出name的话,则不会输出任何东西。
    2. **构造器的定义及调用**

      在类可以用__construct()来定义构造器。

      class Dog {	public $name;	public $numLegs = 4;	public function __construct($name) {		//构造器可以带形参也可以不带形参		$this->name = $name;		//若要对类内成员赋值,需用$this->成员名 的形式赋值。	}	//……}

      构造器在定义对象时自动被调用。

      $dog = new Dog(“Tom”);echo $dog->name;//输出Tom。
    3. **方法的定义及调用**在类内可以自定义方法。
      class Dog {	public $name;	public $numLegs = 4;	public function __construct($name) {		$this->name = $name;	}	public function greet() {		//方法可以带形参也可以不带		return “Hello, my name is ” . $name . “.”;		//方法可以有返回值也可以没有	}	//……}

      方法的调用类似公有成员的调用,()不可省略。

      $dog = new Dog(“Tom”);echo $dog->greet();//输出Hello, my name is Tom.
  16. **php对象(2)**
    1. **类的继承**

      php支持类的继承。

      class Shape {public $hasSides = true;}class Square extends Shape {            	//Square类继承了Shape类}

      通过property_exists()函数可以查看某个类是否包含某个方法或者私有成员。

      $square = new Square();if (property_exists($square, "hasSides")) {	echo "I have sides!";}//有输出,输出I have sides!

      php的类继承与C++中的类继承一样,子类会继承父类的所以公有成员和方法。

    2. **重写父类方法,`final`**

      php支持在子类中重写父类中的方法,并且在调用时调用的为子类重写后的方法。

      class Vehicle {	public function honk() {		return "HONK HONK!";	}}class Bicycle extends Vehicle {	public function honk() {		//父类方法的重写		return "Beep beep!";	}}

      调用子类方法

      $bicycle = new Bicycle();echo $bicycle->honk();//将输出Beep beep!

      若想要在子类重写后,仍调用的为父类继承来的方法。只需在父类方法前增加final关键字。若将上面父类Vehicle中的honk方法改为如下形式,其他保持不变。

      final public function honk() {	//……}

      最终程序将输出HONK HONK!。

    3. **`const`和`::`**

      php类中支持const常量,即定义时就需要赋值,一旦赋值后不允许修改。

      class Cat {	const numLegs = 4;	//不需要添加$符号}

      对于const常量的访问,不能与之前公有成员的访问方法一样,而应该用::来访问。因为常量是针对每个类而言,所以应采取如下形式访问。

      echo Cat::numLegs;//输出4
    4. **`static`**

      php类支持静态方法和静态变量,允许用户在不创建对象而可以访问成员和方法。

      class Person {	public static $isAlive = "Yep!"	public static function greet() {		echo "Hello there!";	}}

      调用时,采取如下形式访问。

      echo Person::$isAlive;//输出Yep!Person::greet();//输出Hello there!
Statement
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
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)