php语法,字符串类型?
php语法,有没有规定:定义字符串时,字符串一定要加单引号,双引号或不加任何符号。
如:
$x=123;
$y="123";
$z='123';
这3个变量,它们是不是一样呢?
回复讨论(解决方案)
第一个是整型 后两个一样,是字符串型.
但是纯数字的字符串在PHP里边有时候也可以当数字用
比如$x = "123";
echo $x*3;
结果是369
可以用getType()函数来检查类型
$x=123;
这个语句是将整形量赋给了$x
$x="123";和$x='123'的赋值操作结果是一样的
都是将字符串"123" 赋给了$x 是字符串类型的
至于单引号和双引号的解释请看此帖
http://bbs.csdn.net/topics/390535456
在没有任何符号的情况下,PHP是怎样判断它是什么类型?
$x=123;
$x=abc;
它们的类型相同么?
$x = 123;
毫无异议,平衡品会视为整数
echo gettype($x); //integer
var_dump($x); //int(123)
$x = abc;
php 先检查这个 abc 是否为常量,如果是就取常量 abc 的值
否则发出警告 Notice>: Use of undefined constant abc - assumed 'abc'
然后解释为字符串
echo gettype($x); //string
var_dump($x); //string(3) "abc"
看到xu大也用拼音输入法 顿时很开心
$x = 123;
毫无异议,平衡品会视为整数
echo gettype($x); //integer
var_dump($x); //int(123)
$x = abc;
php 先检查这个 abc 是否为常量,如果是就取常量 abc 的值
否则发出警告 Notice>: Use of undefined constant abc - assumed 'abc'
然后解释为字符串
echo gettype($x); //string
var_dump($x); //string(3) "abc"
$x = 123; //自动类型转换为:整型
$x = abc; //自动类型转换为:字符串
对不对?
我已经说的很清楚了
我已经说的很清楚了
不过,看的不是很明白!
如果你非要按“非白即黑”的思维定式去理解,那的确是弄不明白
php 的识别过程是这样的
是数字吗?是 赋值结束
是常量吗?是 赋值结束
那就当做字符串吧
PHP 习惯写法字符型是单引号,变量值是双引号
首先,你写源码的都是文本字符,当php去分析源码时,会按照这个字符的规律来识别相应的数据格式。
$x=123; //1
$y="123"; //2
$z='123'; //3
对于php分析时:
1 会被理解成赋值语句,因为有=这个文本字符,那么左边$x是一个变量标识符,右边是“123”连续的3个文本字符。
检测第一个文本字符是一个数字,就使用int的数据格式去匹配后面的文本字符(简单说就是连续的数字组成,正则表示为\d+)时,发现能匹配上,那么这个变量的就是int类型。
2 同样$y和“"123"”(2个"字符包含的连续文本),那么检测到第一个是",符合string数据格式的双引号规则的开头,那么就用双引号规则(简单说就是开头是",结尾是",并且当中没有"或者"的前面是\,正则表示为"([^"]|\\")+")匹配,发现能匹配上就是string类型
这就是php识别源代码的逻辑,用学名就叫语法分析器,经过了语法分析器以后,就知道你的变量是什么类型。
php是弱类型语言,变量不需要声明类型。
首先,你写源码的都是文本字符,当php去分析源码时,会按照这个字符的规律来识别相应的数据格式。
$x=123; //1
$y="123"; //2
$z='123'; //3
对于php分析时:
1 会被理解成赋值语句,因为有=这个文本字符,那么左边$x是一个变量标识符,右边是“123”连续的3个文本字符。
检测第一个文本字符是一个数字,就使用int的数据格式去匹配后面的文本字符(简单说就是连续的数字组成,正则表示为\d+)时,发现能匹配上,那么这个变量的就是int类型。
2 同样$y和“"123"”(2个"字符包含的连续文本),那么检测到第一个是",符合string数据格式的双引号规则的开头,那么就用双引号规则(简单说就是开头是",结尾是",并且当中没有"或者"的前面是\,正则表示为"([^"]|\\")+")匹配,发现能匹配上就是string类型
这就是php识别源代码的逻辑,用学名就叫语法分析器,经过了语法分析器以后,就知道你的变量是什么类型。
第三个怎分析啊?
php语法规定,字符串一定加双引号或单引号,内容可以是数字,字母,字符。对不对?
如:
$x='a*12'; //对
$x="a*12"; //对
$x=a*12; //语法错误。

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



PHP is a server-side scripting language that is used to develop dynamic websites, web applications, and web programs. PHP has a wide range of applications, and both beginners and experienced developers can benefit from it. This article will provide you with an introductory guide to the basic syntax of PHP. If you want to learn PHP programming and build a solid foundation from scratch, you've come to the right place. The basic structure of PHP. A PHP program contains the following three parts: <?php//PHP code?>& on both sides of the code

With the continuous development of Internet technology, PHP, as a Web programming language, is becoming more and more popular. PHP is widely used and can be developed using PHP from simple static websites to large e-commerce websites. Whether you are a novice who is just starting to learn PHP or a developer who already has some experience, it is very necessary to master some basic concepts. In this article, I will introduce 5 basic concepts that PHP beginners must know. Variables in PHP are containers used to store data. In PHP,

PHP is a widely used server-side programming language often used to build dynamic web pages. However, when writing PHP scripts, you often encounter various syntax errors and warnings. These errors and warnings can cause your code to not run properly or cause problems, so it's important to resolve them. This article will introduce common problems in PHP: syntax errors and warnings, and provide effective methods on how to solve these problems. Syntax Errors Syntax errors are a very common problem when writing scripts in PHP. Syntax errors may be due to the following reasons

PHP is a language widely used in web development, and its popularity and usage rate are quite high. Many beginners will encounter some difficulties when learning PHP programming, such as not knowing how to improve their programming level. Below we will introduce some methods to make it easier for you to improve your PHP programming level. Learn the latest technology Internet technology updates very quickly, and PHP is no exception. If you want to become an excellent PHP programmer, you must first learn the latest PHP technology and master the latest Web development technology, such as M

PHP entry to advanced - learn basic syntax and concepts Introduction: PHP (Hypertext Preprocessor) is a popular server-side scripting language widely used in Web development. In this article, we will start from the entry level, gradually learn the basic syntax and concepts of PHP in depth, and provide code examples for your reference. 1. PHP installation and configuration Before starting to learn PHP, you first need to install PHP on your machine. You can download it from the official website (https://w

PHP is a widely used server-side scripting language for developing dynamic web pages and web applications. Many websites and applications use PHP as their back-end development language, so having PHP development skills and experience is crucial to building efficient functionality. In this article, I will share some experiences and tips I have learned in PHP development, aiming to help developers improve their coding skills and ability to build efficient functions. Pick the right development tools and frameworks Before starting PHP development, pick the right development tools and frameworks

The arrow symbol (->) in PHP is a very important operator used to access the properties and methods of objects. This article will provide an in-depth analysis of the functions and characteristics of PHP arrow symbols, and provide specific code examples to help readers better understand their usage. In PHP, the arrow symbol (->) is mainly used to access the properties and methods of objects. When we create an object, we can access the object's properties or call the object's methods through arrow symbols. First, let's look at a simple example, create a class and instantiate

PHP is widely used in web development and is a server-side scripting language. Before learning PHP, we need to have basic knowledge of HTML and CSS. The following will introduce the basic syntax of PHP that beginners must master. Variables Variables are defined and assigned using the $ symbol in PHP, such as $variable="helloworld";. Variable names must start with the $ symbol and can only contain letters, numbers, and underscores. Variables in PHP have the characteristics of dynamic type and can store numbers and strings.
