Home php教程 php手册 第四章_PHP基本语法

第四章_PHP基本语法

Jun 13, 2016 am 10:17 AM
php Basic support data program First Chapter Four type grammar

1.第一个PHP程序

<span 1</span> <span //</span><span 弱爆了有木有</span>
<span 2</span> <?<span php
</span><span 3</span>     <span echo</span> "<span Hello PHP!';
</span><span 4</span> ?>
Copy after login

2.PHP共支持8种基本数据理性,包括4种标量类型:boolean(布尔型)、integer(整型)、float(浮点型,也称为double)、string(字符串),2种复合类型:array(数组)、object(对象),2种特殊类型:resource(资源)、NULL(空类型)

3.函数var_dump可以用来打印变量的相关信息。

<span 1</span> <?<span php
</span><span 2</span>     <span var_dump</span>((bool) <span false</span><span );
</span><span 3</span>     <span var_dump</span>((bool) 0<span );
</span><span 4</span>     <span var_dump</span>((bool) 0.0<span );
</span><span 5</span>     <span var_dump</span>((bool) ''<span );
</span><span 6</span>     <span var_dump</span>((bool) "0"<span );
</span><span 7</span>     <span var_dump</span>((bool) <span array</span><span ());
</span><span 8</span>     <span var_dump</span>((bool) <span NULL</span><span );
</span><span 9</span> ?>
Copy after login

4.关于整型。

  在32位操作系统中,整型数的有效范围是-2147483648~+2147483647。如果超过了这个范围,将会被PHP引擎解释成浮点数(float)。

  用八进制加0,用十六进制加0x(是数字0而非字母O)

<span  1</span> <?<span php
</span><span  2</span>     <span $dec_int</span>=63<span ;
</span><span  3</span>     <span var_dump</span>(<span $dec_int</span><span );
</span><span  4</span>     
<span  5</span>     <span $oct_int</span>=077<span ;
</span><span  6</span>     <span var_dump</span>(<span $oct_int</span><span );
</span><span  7</span>     
<span  8</span>     <span $hex_int</span>=0x3f<span ;
</span><span  9</span>     <span var_dump</span>(<span $hex_int</span><span );
</span><span 10</span> ?>
Copy after login

5.字符串型

  有3种定义字符串的方式,分别是单引号(‘)、双引号(“)和界定符(<<<)。前面2个的区别是,假如字符串中包含变量名,双引号中的变量名会被实际值替代,而单引号中的变量名会按普通字符直接输出。

<span 1</span> <?<span php
</span><span 2</span>     <span $name</span>='麦兜'<span ;
</span><span 3</span>     <span $age</span>=19<span ;
</span><span 4</span>     <span echo</span> '本人大名$name,年方$age'<span ;
</span><span 5</span>     <span echo</span> '<p>'<span ;
</span><span 6</span>     <span echo</span> "本人大名<span $name</span>,年方<span $age</span>"<span ;
</span><span 7</span> ?>
Copy after login

  关于界定符使用如下:

<span 1</span> <?<span php
</span><span 2</span>     <span echo</span> <<<<span MY_STR
</span><span 3</span> <span     我是PHP初学者
</span><span 4</span> <span MY_STR;      //最后结尾的标识符前面不能有空格或者缩进,必须顶咯
</span><span 5</span> ?>
Copy after login

6.对象类型

<span  1</span> <?<span php
</span><span  2</span> <span class</span><span  Person {
</span><span  3</span>     <span public</span> <span $name</span><span ;
</span><span  4</span>     <span private</span> <span $age</span><span ;
</span><span  5</span>     
<span  6</span>     <span function</span> Person(<span $name</span>,<span $age</span><span ){
</span><span  7</span>         <span $this</span>->name=<span $name</span><span ;
</span><span  8</span>         <span $this</span>->age=<span $age</span><span ;
</span><span  9</span> <span     }
</span><span 10</span>     
<span 11</span>     <span function</span><span  getInfo(){
</span><span 12</span>         <span echo</span> '我叫'.<span $this</span>->name.'<br>我今年'.<span $this</span>->age.'岁了'<span ;
</span><span 13</span> <span     }
</span><span 14</span> <span }
</span><span 15</span>     <span $boy</span> = <span new</span> Person("kimi",5<span );
</span><span 16</span>     <span $boy</span>-><span getInfo();
</span><span 17</span> ?>    
Copy after login

7.类型转换

  print_r() 显示关于一个变量的易于理解的信息。如果给出的是 stringintegerfloat,将打印变量值本身。如果给出的是 array,将会按照一定格式显示键和元素

  

<span  1</span> <?<span php
</span><span  2</span>     <span $temp_var</span>=3.141592653<span ;
</span><span  3</span>     <span $var1</span>=(<span string</span>)<span $temp_var</span><span ;
</span><span  4</span>     <span echo</span> "<p><span $var1</span>"<span ;
</span><span  5</span>     
<span  6</span>     <span $temp_var</span>='Kimi'<span ;
</span><span  7</span>     <span $var2</span>=(<span array</span>)<span $temp_var</span><span ;
</span><span  8</span>     <span echo</span> '<p>'<span ;
</span><span  9</span>     <span print_r</span>(<span $var2</span><span );
</span><span 10</span>     
<span 11</span>     <span $temp_var</span>='Cindy'<span ;
</span><span 12</span>     <span $var3</span>=(<span unset</span>)<span $temp_var</span><span ;
</span><span 13</span>     <span echo</span> '<p>'<span ;
</span><span 14</span>     <span var_dump</span>(<span $var3</span><span );
</span><span 15</span> ?>
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 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 and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 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 and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles