-
php基本简介
-
为何要学习php
通过上网查资料,了解了基本的php知识,并知道了php的优缺点。php是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。php 独特的语法混合了C、Java、Perl以及php自创的语法。它可以比CGI或者Perl更快速地执行动态网页。用php做出的动态页面与其他的编程语言相比,php是将程序嵌入到HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成html标记的CGI要高许多;php还可以执行编译后代码,编译可以达到加密和优化代码运行,使代码运行更快。
php和html,配置php环境
-
- php语法支持html语法,可以在php的代码之中完美的嵌套标签语言。PHP是将程序嵌入到HTML文档中去执行,执行效率比完全生成htmL标记的CGI要高许多。
- 跟着网上教程学会了php环境的配置,并成功运行第一个php文件。
-
基础php:
echo,print,strings,arithmetic,variables,semicolons,comments - 了解echo和print的区别:echo可以输出多个值,每个值之间用逗号隔开,而print只能输出一个值。
- 在输出string类型时,echo和print均可,多个字符串连接在一起时可用英式点即”.”相连。
- arithmetic即在输出时支持算术运算,比如echo 2 * 3;,即将输出6。
- variables,php的变量没有类型而言,换句话说即时自动匹配类型,定义方式类似$name = “wsy”;,即每个变量名之前只需要加一个$符号,剩下为自己定义的名字即可。变量在被初次赋值后才被定义,所以不需要$name;如此定义变量。
- 每条php语句都需要以分号结尾,这点与C/C++一样
- comments,注释也与C/C++的注释一样,可是使用//,也可以使用/**/。
-
php条件控制
-
比较符号
php的比较符号类似C/C++,也分为大于(>),小于(=),小于等于(
-
if和else和elseif
php中的if语句与C/C++很类似,也在()内添加条件,同时也支持这样if(1)或者if(true)的简化写法。
-
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;
-
-
php数组
-
数组的定义
因为php对于变量是自动匹配类型,则php的数组对于不同类型的变量也是一视同仁,即php的数组可以添加任何类型的变量在一个数组当中。
$a = array(“Tom”, “Jim”, 1, 2);
如此定义了一个名为a的数组,含有4个元素,2个string类型,2个数字类型。 -
数组元素的获取,[],{}
通过数组下标(同C/C++也是从0开始)来访问数组的元素有2种方法,一个是用[],另一个是用{}。这2个方法是一样的,不管用哪种方法都可以访问数组元素。
echo $a[2]; //输出1echo $a{1}; //输出Jim
-
数组元素的修改
php支持对数组元素的修改,修改方法即通过[]或者{}访问对应的数组元素,然后将其赋予新的值即可。
$a[1] = “TTT”;echo $a{1}; //输出TTT,因为已经修改了。
-
数组元素的删除
php支持对数组中某个元素的删除或对整个数组的删除的功能。通过unset();即可删除对应的元素。
unset($a[1]); //删除了数组中的第二个元素,现在数组包含Tom,1,2unset($a); //删除了整个数组
-
-
php循环
**`for`循环**
php中的for循环与C/C++相似,支持++操作符,但不支持+=,所以对于不是+1的循环,需要写成$i = $i + 3;
for ($i = 0; $i <= 100; $i = $i + 10) { //……}
**`foreach`循环**
当需要循环整个数组时,写for循环可能会过于繁琐,所以php提供了一种特殊的循环方法,即foreach循环。写法如下:
$a = array(“Tom”, “Jim”, 1, 2, 3);foreach ($a as $i) { echo $i;}
这样就可以将php数组中的所有元素都输出了。
foreach (数组名 as 变量名),接下来对于数组中每个元素操作即对用户自己取变量名的变量操作即可,但不支持修改功能,类似与C/C++中的传值与传址的区别。**`while`循环,`endwhile`**
php中的while循环有2种写法,普通的写法是都所知道的。
while () { //……}
类似于之前的switch,php中也有另外一种写法。
while (): //……endwhile;
同if/else一样,while同时也支持这样while(1)或者while(true)的简化写法。
**`do-while`循环**
php中的do-while循环与C/C++一样。
do { //……} while ();
do-while也支持这样do-while(1)或者do-while(true)的简化写法。
**循环嵌套**
php中支持各种循环的相互嵌套。
-
php函数(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
**关于数学的相关函数**
round(number(, n))rand(min, max)
round函数用于对浮点数保留小数。
round(number):这样写,默认对number保留为整数。
round(number, n):这样写,对number保留n位小数。
rand(min, max):此函数用于求给定范围[min, max]中的随机数。**关于数组的相关函数**
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
-
**php函数(2)**
**自定义函数**
php中允许用户定义自己所需的不同的函数,但不需要写函数类型。
function Template() { //……}
function 代表定义的是一个函数,Template为函数名,用户可以随意定义,不需要考虑函数的类型。
**带形参的自定义函数**
在函数定义时,()之中可以为空,也可以添加形参,即称为带有形参的函数。
function aboutMe($name, $age) { echo "Hello! My name is " . $name . ", and I am " . $age . " years old.";}
此函数定义了两个形参,分别为$name和$age。调用时,aboutMe("wsy", 22);即可,也可以传入其他变量名。
**自定义函数的返回值**
php的函数虽然没有函数类型,但是可以返回任意类型的值。
function returnName() { return "wsy";}
return后可以跟变量也可直接跟常量。
**php对象(1)**
**类的定义、对象的定义**
同C++中类的定义相同,php的类定义如下。
class Dog { public $name; public $numLegs = 4; //可以定义公有成员,可以对其赋值也可以对其不赋值 //……}
建立一个类的对象。
$dog = new Dog();
调用公有成员。echo $dog->numLegs;//name因为未赋值,若要输出name的话,则不会输出任何东西。
**构造器的定义及调用**
在类可以用__construct()来定义构造器。
class Dog { public $name; public $numLegs = 4; public function __construct($name) { //构造器可以带形参也可以不带形参 $this->name = $name; //若要对类内成员赋值,需用$this->成员名 的形式赋值。 } //……}
构造器在定义对象时自动被调用。
$dog = new Dog(“Tom”);echo $dog->name;//输出Tom。
**方法的定义及调用**在类内可以自定义方法。
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.
**php对象(2)**
**类的继承**
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++中的类继承一样,子类会继承父类的所以公有成员和方法。
**重写父类方法,`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!。
**`const`和`::`**
php类中支持const常量,即定义时就需要赋值,一旦赋值后不允许修改。
class Cat { const numLegs = 4; //不需要添加$符号}
对于const常量的访问,不能与之前公有成员的访问方法一样,而应该用::来访问。因为常量是针对每个类而言,所以应采取如下形式访问。
echo Cat::numLegs;//输出4
**`static`**
php类支持静态方法和静态变量,允许用户在不创建对象而可以访问成员和方法。
class Person { public static $isAlive = "Yep!" public static function greet() { echo "Hello there!"; }}
调用时,采取如下形式访问。
echo Person::$isAlive;//输出Yep!Person::greet();//输出Hello there!

Laravel simplifie la gestion des données de session temporaires à l'aide de ses méthodes de flash intuitives. Ceci est parfait pour afficher de brefs messages, alertes ou notifications dans votre application. Les données ne persistent que pour la demande ultérieure par défaut: $ demande-

L'extension PHP Client URL (CURL) est un outil puissant pour les développeurs, permettant une interaction transparente avec des serveurs distants et des API REST. En tirant parti de Libcurl, une bibliothèque de transfert de fichiers multi-protocol très respectée, PHP Curl facilite Efficient Execu

La journalisation PHP est essentielle pour surveiller et déboguer les applications Web, ainsi que pour capturer des événements critiques, des erreurs et un comportement d'exécution. Il fournit des informations précieuses sur les performances du système, aide à identifier les problèmes et prend en charge le dépannage plus rapide

Laravel fournit une syntaxe de simulation de réponse HTTP concise, simplifiant les tests d'interaction HTTP. Cette approche réduit considérablement la redondance du code tout en rendant votre simulation de test plus intuitive. L'implémentation de base fournit une variété de raccourcis de type de réponse: Utiliser illuminate \ support \ faades \ http; Http :: faux ([[ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Voulez-vous fournir des solutions instantanées en temps réel aux problèmes les plus pressants de vos clients? Le chat en direct vous permet d'avoir des conversations en temps réel avec les clients et de résoudre leurs problèmes instantanément. Il vous permet de fournir un service plus rapide à votre personnalité

L'article traite de la liaison statique tardive (LSB) dans PHP, introduite dans PHP 5.3, permettant une résolution d'exécution de la méthode statique nécessite un héritage plus flexible. Problème main: LSB vs polymorphisme traditionnel; Applications pratiques de LSB et perfo potentiel

L'article examine l'ajout de fonctionnalités personnalisées aux cadres, en se concentrant sur la compréhension de l'architecture, l'identification des points d'extension et les meilleures pratiques pour l'intégration et le débogage.

Alipay Php ...


Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

MinGW - GNU minimaliste pour Windows
Ce projet est en cours de migration vers osdn.net/projects/mingw, vous pouvez continuer à nous suivre là-bas. MinGW : un port Windows natif de GNU Compiler Collection (GCC), des bibliothèques d'importation et des fichiers d'en-tête librement distribuables pour la création d'applications Windows natives ; inclut des extensions du runtime MSVC pour prendre en charge la fonctionnalité C99. Tous les logiciels MinGW peuvent fonctionner sur les plates-formes Windows 64 bits.

Télécharger la version Mac de l'éditeur Atom
L'éditeur open source le plus populaire

Dreamweaver CS6
Outils de développement Web visuel

VSCode Windows 64 bits Télécharger
Un éditeur IDE gratuit et puissant lancé par Microsoft