Home php教程 php手册 php学习笔记之基础知识

php学习笔记之基础知识

Jun 06, 2016 pm 08:17 PM
basic knowledge study notes

这篇文章主要介绍了php学习笔记的基础知识部分,需要的朋友可以参考下

php学习至今一年有余,笔记积累挺多的,也挺杂的,写篇文章整理一下吧。

php基础部分

PHP 输出文本的基础指令:echo 和 print。

echo和print的区别

echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)

echo 输出一个或者多个字符串。
print 只能打印出简单类型变量的值(如int,string)
print_r 可以打印出复杂类型变量的值(如数组,对象)

var_dump和print_r的区别

var_dump返回表达式的类型和值,而print_r仅返回结果,相比调试代码使用var_dump更便于阅读。

变量

变量用于存储值,比如数字、文本字符串或数组。PHP 中的所有变量都是以 $ 符号开始的。
PHP 变量名称对大小写敏感!

PHP 有三种不同的变量作用域:

local(局部)
global(全局)
static(静态)

函数之外声明的变量拥有 Global 作用域,只能在函数以外进行访问。

函数内部声明的变量拥有 LOCAL 作用域,只能在函数内部进行访问。

global 关键词用于访问函数内的全局变量。

PHP static 关键词

通常,当函数完成/执行后,会删除所有变量。不过,有时我需要不删除某个局部变量。实现这一点需要更进一步的工作。

要完成这一点,请在您首次声明变量时使用 static 关键词:

function myTest() {
   static $x=-1;
   echo $x;
   $x--;
}
myTest();//-1
echo "
";
myTest();//-2
echo "
";
myTest();//-3
?>

php类型

php类型:**PHP 支持八种原始类型。**

布尔型

要指定一个布尔值,使用关键字 TRUE 或 FALSE。两个都是大小写不敏感的。

整型

我们可以使用(int)来将小数强制类型转换成整数。

    var_dump((int)(26/3));//int(8)
?>

数组

php中有三种数组:

索引数组:就是下标是顺序整数作为作为索引(比如第几排第几列)$class[5]
关联数组:就是下标是字符串作为索引(比如名字)$class2["zhangsan"]
多维数组 - 包含一个或多个数组的数组

下标要嘛是整数,要么是字符串。

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);
// 自 PHP 5.4 起
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

数组单元可以通过 array[key] 语法来访问。
Note: 这并不意味着总是给键名加上引号。用不着给键名为常量或变量的加上引号,否则会使 PHP 不能解析它们。

数组运算符 例子 名称 结果$a + $b 联合 $a 和 $b 的联合$a == $b 相等 如果 $a 和 $b 具有相同的键/值对则为 TRUE$a === $b 全等 如果 $a 和 $b 具有相同的键/值对并且顺序和类型都相同则为 TRUE$a != $b 不等 如果 $a 不等于 $b 则为 TRUE$a $b 不等 如果 $a 不等于 $b 则为 TRUE$a !== $b 不全等 如果 $a 不全等于 $b 则为 TRUE

+ 运算符把右边的数组元素附加到左边的数组后面,两个数组中都有的键名,则只用左边数组中的,右边的被忽略。

对象

要初始化一个对象,用 new 语句将对象实例到一个变量中。

常用函数

strlen() 函数用于计算字符串的长度。
strpos() 函数用于在字符串内检索一段字符串或一个字符。

常量

可以用 define() 函数来定义常量。一个常量一旦被定义,就不能再改变或者取消定义。
常用的魔术常量:

定义常量例子:

define("poems" , "Homeric epic");
echo poems ;//outputs "Homeric epic"
?>

php字符串运算符

在 PHP 中,只有一个字符串运算符。
并置运算符 (.) 用于把两个字符串值连接起来。如:echo "a= ".$a."
";
左边将字符串文字"a="与变量$a的值连接,第二处是与换行符"
"连接

php函数

函数只有在被调用时才会被执行,这点和js是一样的,同样,函数定义也是以function关键字开头的。

    function sum($x,$y){
        $z=$x + $y;
        return $z;
    }
    echo "-2+10= ".sum(-2,10);//outputs "-2+10=8"
?>

当没有return语句时,以上将会变成"-2+10=";

流程控制

在这里,只讲下foreach语句。

foreach语句遍历输出数组:
语法:

foreach (array_expression as $value){ statement}; foreach (array_expression as $key => $value){ statement};

参数array_expression是指定要遍历的数组,$value是数组的值

     $actors [0] ="Marry";
     $actors [1] ="Lorry";
     $actors [2] = "mike";
     foreach ($actors as $values){
     echo "Name:$values
";
}
?>

以上代码将输出:
Name:Marry
Name:Lorry
Name:mike

两个重要的魔术方法

1. __set( )方法:这个方法用来为私有成员属性设置值的,有两个参数,第一个参数为你 要为设置值的属性名,第二个参数是要给属性设置的值,没有返回值。 2. __get()方法:这个方法用来获取私有成员属性值的,有一个参数,参数传入你要获取的成员属性的名称,返回获取的属性值,这个方法不用我们手工的去调用

php中方法不区分大小写

require(dirname(__FILE__).'/global.php'); //引入全局文件 require(dirname(__FILE__).'/config.ini.php'); //引入基本配置文件

对象运算符和双冒号运算符

在类的成员方法里面,可以用 ->(对象运算符):$this->property(其中 property 是该属性名)这种方式来访问非静态属性。
静态属性则是用 ::(双冒号):self::$property 来访问。

=>和->

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

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

Learn to completely uninstall pip and use Python more efficiently Learn to completely uninstall pip and use Python more efficiently Jan 16, 2024 am 09:01 AM

No more need for pip? Come and learn how to uninstall pip effectively! Introduction: pip is one of Python's package management tools, which can easily install, upgrade and uninstall Python packages. However, sometimes we may need to uninstall pip, perhaps because we wish to use another package management tool, or because we need to completely clear the Python environment. This article will explain how to uninstall pip efficiently and provide specific code examples. 1. How to uninstall pip The following will introduce two common methods of uninstalling pip.

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Feb 19, 2024 pm 10:10 PM

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? Mar 25, 2024 pm 03:20 PM

As a lifestyle sharing platform, Xiaohongshu covers notes in various fields such as food, travel, and beauty. Many users want to share their notes on Xiaohongshu but don’t know how to do it. In this article, we will detail the process of posting notes on Xiaohongshu and explore how to block specific users on the platform. 1. How to publish notes tutorial on Xiaohongshu? 1. Register and log in: First, you need to download the Xiaohongshu APP on your mobile phone and complete the registration and login. It is very important to complete your personal information in the personal center. By uploading your avatar, filling in your nickname and personal introduction, you can make it easier for other users to understand your information, and also help them pay better attention to your notes. 3. Select the publishing channel: At the bottom of the homepage, click the "Send Notes" button and select the channel you want to publish.

See all articles