第四章_PHP基本语法
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> ?>
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> ?>
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> ?>
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> ?>
关于界定符使用如下:
<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> ?>
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> ?>
7.类型转换
print_r() 显示关于一个变量的易于理解的信息。如果给出的是 string、integer 或 float,将打印变量值本身。如果给出的是 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> ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

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

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

In this chapter, we are going to learn the following topics related to routing ?

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

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

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
