1. 首页
  2. php

PHP – 变量类型

在PHP程序中间存储信息的主要方法是使用变量。

PHP - 变量类型

以下是PHP中最重要的变量知识。

  • PHP中的所有变量都以前导美元符号($)表示。
  • 变量的值是其最近赋值的值。
  • 变量分配有=运算符,左侧是变量,右侧是要计算的表达式。
  • 变量可以但不需要在赋值之前声明。
  • PHP中的变量没有内在类型 – 变量事先不知道它是否将用于存储数字或字符串。
  • 分配前使用的变量具有默认值。
  • PHP可以在必要时自动将类型从一个转换为另一个。
  • PHP变量类似于Perl。

PHP总共有八种数据类型,我们用它来构造我们的变量 –

  • 整数 – 是整数,没有小数点,如4195。
  • 双打 – 是浮点数,如3.14159或49.1。
  • 布尔值 – 只有两个可能的值,无论是真还是假。
  • NULL – 是一种特殊类型,只有一个值:NULL。
  • 字符串 – 是字符序列,如“PHP支持字符串操作”。
  • 数组 – 是其他值的命名和索引集合。
  • 对象 – 是程序员定义的类的实例,它们可以打包特定于类的其他类型的值和函数。
  • 资源 – 是包含对PHP外部资源(例如数据库连接)的引用的特殊变量。

前五个是简单类型,接下来的两个(数组和对象)是复合的 – 复合类型可以打包任意类型的其他任意值,而简单类型则不能。

我们将在本章中仅解释简单的数据类型。数组和对象将单独解释。

整型

它们是整数,没有小数点,如4195.它们是最简单的类型。它们对应于简单的整数,包括正数和负数。整数可以分配给变量,也可以在表达式中使用,如下所示 –

$int_var = 12345;
$another_int = -12345 + 12345;

整数可以是十进制(基数10),八进制(基数8)和十六进制(基数16)格式。十进制格式是默认格式,八进制整数用前导0指定,十六进制格式有前导0x。

对于大多数常见平台,最大整数是(2 ** 31.1)(或2,147,483,647),最小(最负)整数是。(2 ** 31.1)(或.2,147,483,647)。

双打

他们喜欢3.14159或49.1。默认情况下,使用所需的最小小数位数打印双打。例如,代码 –

<?php
   $many = 2.2888800;
   $many_2 = 2.2111200;
   $few = $many + $many_2;
   
   print("$many + $many_2 = $few <br>");
?>

它产生以下浏览器输出 –

2.28888 + 2.21112 = 4.5

布尔

它们只有两个可能的值,无论是真还是假。PHP提供了一些常量,特别是用作布尔值:TRUE和FALSE,可以像这样使用 –

if (TRUE)
   print("This will always print<br>");

else
   print("This will never print<br>");

将其他类型解释为布尔值

以下是确定不属于布尔类型的任何值的“真值”的规则 –

  • 如果该值是数字,则如果恰好等于零则为假,否则为真。
  • 如果该值是字符串,则如果字符串为空(具有零个字符)或字符串为“0”则为false,否则为true。
  • NULL类型的值始终为false。
  • 如果值是数组,则如果它不包含其他值则为false,否则为true。对于对象,包含值意味着具有已分配值的成员变量。
  • 有效资源为true(尽管某些在成功时返回资源的函数将在失败时返回FALSE)。
  • 不要使用double作为布尔值。

当在布尔上下文中使用时,以下每个变量都在其名称中嵌入了真值。

$true_num = 3 + 0.14159;
$true_str = "Tried and true"
$true_array[49] = "An array element";
$false_array = array();
$false_null = NULL;
$false_num = 999 - 999;
$false_str = "";

空值

NULL是一种特殊类型,只有一个值:NULL。要给变量一个NULL值,只需像这样分配它 –

$my_var = NULL;

特殊常量NULL按惯例大写,但实际上它不区分大小写; 你也可以打字 –

$my_var = null;

已赋值为NULL的变量具有以下属性 –

  • 它在布尔上下文中计算为FALSE。
  • 使用IsSet()函数测试时,它返回FALSE。

字符串

它们是字符序列,例如“PHP支持字符串操作”。以下是字符串的有效示例

$string_1 = "This is a string in double quotes";
$string_2 = 'This is a somewhat longer, singly quoted string';
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters

单引号字符串几乎按字面处理,而双引号字符串用其值替换变量以及特别解释某些字符序列。

<?php
   $variable = "name";
   $literally = 'My $variable will not print!';
   
   print($literally);
   print "<br>";
   
   $literally = "My $variable will print!";
   print($literally);
?>

这将产生以下结果 –

My $variable will not print!
My name will print

字符串长度没有人为限制 – 在可用内存的范围内,你应该能够制作任意长的字符串。

由双引号分隔的字符串(如“this”中所示)由PHP以下两种方式进行预处理 –

  • 以反斜杠(\)开头的某些字符序列将替换为特殊字符
  • 变量名(以$开头)将替换为其值的字符串表示形式。

逃逸序列替换是 –

  • \ n由换行符替换
  • \ r \ n由回车符替换
  • \ t由制表符替换
  • \ $被美元符号本身取代($)
  • \“被一个双引号(”)取代
  • \\由单个反斜杠(\)替换

这里是文件

您可以使用此处文档为单个字符串变量分配多行 –

<?php
   $channel =<<<_XML_
   
   <channel>
      <title>What's For Dinner</title>
      <link>http://menu.example.com/ </link>
      <description>Choose what to eat tonight.</description>
   </channel>
   _XML_;
   
   echo <<<END
   This uses the "here document" syntax to output multiple lines with variable 
   interpolation. Note that the here document terminator must appear on a line with 
   just a semicolon. no extra whitespace!
   

   END;
   
   print $channel;
?>

这将产生以下结果 –

This uses the "here document" syntax to output
multiple lines with variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!

<channel>
<title>What's For Dinner<title>
<link>http://menu.example.com/<link>
<description>Choose what to eat tonight.</description>

可变范围

范围可以定义为变量对声明它的程序的可用性范围。PHP变量可以是四种范围类型之一 –

  • 局部变量
  • 功能参数
  • 全局变量
  • 静态变量

变量命名

命名变量的规则是 –

  • 变量名称必须以字母或下划线字符开头。
  • 变量名称可以包含数字,字母,下划线,但不能使用+, – ,%,(,)等字符。&等

变量没有大小限制。

原创文章,作者:零久SEO,如若转载,请注明出处:http://www.seo0j.com/279.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论列表(15条)

  • kobe shoes 2020年2月22日 下午8:36

    I together with my buddies were studying the great tips located on the blog then instantly I got a terrible feeling I never expressed respect to the site owner for those tips. Most of the young men came certainly happy to read them and have now truly been making the most of them. Thank you for truly being so considerate as well as for getting such incredible guides most people are really desirous to know about. Our sincere regret for not saying thanks to you sooner.

  • yeezy supply 2020年2月25日 上午9:42

    Thank you a lot for giving everyone a very remarkable opportunity to read in detail from this web site. It really is very pleasant and as well , full of a great time for me and my office co-workers to visit your blog really 3 times in 7 days to see the newest issues you have got. And definitely, I’m certainly impressed with all the beautiful tips and hints served by you. Certain 2 areas in this article are certainly the best I have ever had.

  • louboutin shoes 2020年2月28日 上午5:06

    My spouse and i have been satisfied Raymond could complete his inquiry with the ideas he had when using the blog. It is now and again perplexing to simply happen to be freely giving strategies some others might have been making money from. And we all realize we’ve got the blog owner to thank for that. The main explanations you have made, the straightforward site menu, the relationships your site assist to engender – it’s mostly extraordinary, and it is assisting our son in addition to us do think this subject matter is cool, which is tremendously fundamental. Thanks for all!

  • jordan 11 2020年3月1日 下午7:05

    I would like to show my thanks to this writer just for rescuing me from such a challenge. As a result of browsing through the world wide web and getting views that were not productive, I was thinking my life was over. Existing devoid of the solutions to the difficulties you’ve resolved by way of the website is a critical case, and those that might have in a wrong way damaged my career if I had not encountered your blog. That knowledge and kindness in dealing with a lot of stuff was excellent. I am not sure what I would have done if I hadn’t discovered such a subject like this. I’m able to at this point look ahead to my future. Thanks very much for the reliable and amazing guide. I will not think twice to propose the website to anybody who ought to have care about this subject.

  • adidas tubular 2020年3月4日 上午8:57

    I am commenting to let you know of the fantastic experience my wife’s child enjoyed checking your web site. She discovered a good number of details, which included what it is like to possess a marvelous helping heart to have the others really easily know precisely a variety of grueling issues. You truly surpassed her expectations. Many thanks for showing these beneficial, trusted, educational and fun tips on your topic to Gloria.

  • yeezy shoes 2020年3月7日 上午2:31

    I have to express some thanks to the writer for rescuing me from this type of issue. As a result of surfing through the search engines and coming across tips that were not productive, I thought my life was gone. Living without the approaches to the difficulties you have solved as a result of the guideline is a critical case, and those that would have badly damaged my entire career if I had not discovered your blog. Your good talents and kindness in taking care of all the stuff was helpful. I am not sure what I would’ve done if I hadn’t encountered such a solution like this. It’s possible to at this moment look ahead to my future. Thanks very much for this specialized and results-oriented guide. I will not think twice to refer the website to any person who needs and wants assistance about this issue.

  • nike air max 270 2020年3月9日 上午8:50

    I truly wanted to type a small message in order to express gratitude to you for some of the nice tips you are giving out at this site. My prolonged internet search has now been compensated with good quality tips to exchange with my pals. I would point out that most of us visitors are really endowed to be in a decent community with very many wonderful professionals with very beneficial tactics. I feel quite happy to have discovered the web pages and look forward to some more excellent moments reading here. Thanks a lot again for everything.

  • fitflop 2020年3月13日 上午2:32

    My husband and i have been really fulfilled when Edward could do his research through the precious recommendations he discovered through the web pages. It is now and again perplexing to just possibly be giving for free helpful tips which some other people have been selling. Therefore we figure out we have you to appreciate for this. All of the illustrations you have made, the straightforward web site menu, the friendships you will assist to promote – it is all fabulous, and it is aiding our son in addition to us feel that this theme is enjoyable, which is very serious. Many thanks for all the pieces!

  • nike react 2020年3月16日 上午2:04

    I would like to point out my appreciation for your generosity giving support to those people who actually need guidance on that niche. Your personal commitment to getting the message all-around was amazingly invaluable and have all the time empowered individuals like me to achieve their endeavors. Your insightful tips and hints denotes a whole lot to me and extremely more to my colleagues. Many thanks; from all of us.

  • birkin bag 2020年3月18日 上午9:51

    Thanks so much for providing individuals with an exceptionally memorable opportunity to read articles and blog posts from this blog. It is usually very excellent and full of a lot of fun for me and my office friends to search your site at the least thrice weekly to learn the newest things you will have. And definitely, we are actually pleased with your attractive tactics you serve. Certain two tips in this article are really the most effective I’ve had.

  • yeezys 2020年3月21日 上午6:11

    I simply wanted to thank you very much all over again. I am not sure the things that I would have worked on in the absence of the entire ideas discussed by you regarding this industry. It was actually a real difficult circumstance in my opinion, however , witnessing your specialised way you solved that forced me to cry over delight. I am happy for this guidance and even pray you realize what a great job that you are undertaking teaching the others via your site. I am sure you’ve never come across any of us.

  • kd shoes 2020年3月24日 上午3:14

    I must show some thanks to the writer just for rescuing me from this particular difficulty. Right after exploring throughout the world wide web and meeting ideas which are not powerful, I thought my life was done. Existing without the presence of strategies to the difficulties you have fixed through your good site is a serious case, as well as the ones which might have negatively damaged my entire career if I hadn’t noticed your site. Your primary natural talent and kindness in playing with everything was very useful. I don’t know what I would have done if I had not discovered such a thing like this. It’s possible to at this time look forward to my future. Thank you so much for your professional and sensible help. I will not think twice to recommend your web page to any individual who ought to have assistance on this area.

  • coach factory outlet 2020年3月26日 下午12:12

    I together with my friends came digesting the great hints on your web blog and so at once I got a terrible suspicion I never thanked the site owner for those techniques. All the people had been absolutely happy to study all of them and have in effect quite simply been having fun with these things. Many thanks for getting well considerate as well as for choosing some brilliant tips millions of individuals are really desirous to be informed on. Our sincere apologies for not saying thanks to sooner.

  • supreme clothing 2020年3月30日 上午3:10

    I wish to show my thanks to you for rescuing me from this particular dilemma. Just after checking through the search engines and meeting proposals which are not powerful, I was thinking my life was over. Living minus the approaches to the difficulties you have sorted out through the report is a critical case, as well as ones that would have adversely affected my entire career if I had not come across the blog. That ability and kindness in playing with every part was very helpful. I am not sure what I would have done if I hadn’t come across such a solution like this. I can at this point relish my future. Thanks so much for the expert and amazing guide. I won’t think twice to refer your blog post to any person who needs guidance on this issue.

  • nike foamposite 2020年4月2日 上午2:51

    I in addition to my pals were going through the best information from the blog and so at once came up with an awful feeling I had not thanked the blog owner for those secrets. Those people happened to be consequently happy to read them and already have in reality been using these things. Appreciate your being simply helpful and for opting for this form of cool useful guides most people are really needing to understand about. Our sincere regret for not expressing gratitude to sooner.

联系我们

15555333984

在线咨询:点击这里给我发消息

邮件:lxa6@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code