学习笔记_25_03_08
学习笔记_25_03_08
关于VUC上Topic 1 - Reviosn Quiz, Topic 2 - Reviosn Quiz和Topic 3 - Reviosn Quiz的错题总结和相关知识点的整理。
number_format()函数
Qus 01:
To round and format the value of a variable named $number to 3 decimal places anostore it in a variable named $number formatted, you code ...
a) \$number_formatted = number_format(\$number, 3);
b) \$number_formatted = number_format($number, 3, round);
c) \$number_formatted = format_number(\$number, 3);
d) \$number_formatted = format_number(\$number, 3, round);
这道题涉及到了PHP中的number_format()函数,其语法为:
1 |
|
参数 | 描述 |
---|---|
number | 必需,用来填充需要被格式化的变量或数字。 |
decimals | 可选,规定保留几位小数 |
decimalpoint | 可选,规定用作小数点的字符串 |
separator | 可选,规定用作千位分隔符的字符串 |
PHP中的转义字符
Qus 02:
lf you need to print the following line using the echo statement with **double quotes**.
I paid a "ransom" of $rent for my \'landlord \'
What should be the 3rd line in the following code fragment?
1 |
|
a) echo "l paid a "ransom" of \\$rent for my \\'landlord \\'";
b) echo "l paid a \\"ransom\\" of \\$rent for my \\'landlord \\'";
c) echo "l paid a \\"ransom" of $rent for my \\'landlord \\'";
d) echo "l paid a \\"ransom\\" of $rent for my \\\\'landlord \\\\'";
首先需要明确的是在PHP中双引号和单引号用法上的区别:
单引号中的内容会被识别为纯文本,即PHP不会解析其中的变量或者特殊字符。
双引号则相反,它将允许PHP解析字符串中的特殊字符。
1 |
|
因此,在题目中要求我们采用双引号进行输出的时候,为了避免PHP解析其中的变量和特殊字符,我们需要使用转义字符,即\
。即在需要输出的语句中的$
,'
,"
等特殊字符前依次添加一个\
即可。
综上所述,这道题的答案为b)
foreach循环
Qus 03:
Which of the following loop constructs in PHP will print out each team in the array $nbaTeams
a) for ($i= 0; $i< count($nbaTeams); $i++) echo $nbaTeams[ ]."< br>";
b) foreach ($team as $nbaTeams) echo $team."< br>";
c) foreach ($nbaTeams as $team) echo $team."< br>";
d) for ($i= 0; $i< count(nbaTeams); $i++) echo $nbaTeams[ $i]."< br>";
foreach循环只适用于数组,并用于遍历数组中的每个键值对,其语法为:
1 |
|
其有以下几种基本用法:
一、遍历数组的值
1 |
|
输出:
1 |
|
此处的语法和Python中的For循环就很像,可以辅助理解:
1 |
|
二、遍历数组的键值对
1 |
|
输出:
1 |
|
三、反过来修改数组中的值
从PHP5开始,可以通过在$value
前加一个&
来实现修改原数组中的值。
1 |
|
输出:
1 |
|
综上所述,答案选c)
$_GET函数的用法
Questions in this section uses the following URL:
http://localhost/Day2/day2lecture.php?name=John&lname=Smith&title=Mr
Qus 04:
When the URL contains a query string, an HTTP GET request is generated.
With the GET request, the super global array $_GET gets populated with keys andvalues of the URL parameters.
a) True
b) False
Qus 05:
Consider the URL:
localhost/Day3/index.php?fname=John&name=Smith&title=Mr
What the values in the URL could be retrieved as?
Qus 06:
Select all correct answers.
Value 'John' can be retrieved using the keys and values in super global array:
a) $_REQUEST
b) $_GET
c) $_POST
d) $_SERVER
在 PHP 中,预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值。此外,使用这种方法进行传递的信息是对任何人都可见的。这是因为其信息都被包含在URL(Unifrom Resource Locator, 统一资源定位符,会显示在浏览器的地址栏中)里,任何人都可以进行解读。由于URL的限制,GET方法能够传递的信息量是有限的。
在URL被发送到服务器端之后,PHP就可以对其进行解读,表单域的名称及其对应的值就会自动成为$_GET数组中的键值对。
1 |
|
和$_GET相对的,PHP中还有$_POST的用法。前者通常用于从服务器中检索信息,而后者则是用于向服务器中发送数据。特别要注意的是,POST请求会把数据放在HTTP请求的主体当中,故其传递的信息不会在URL中显示。在没有了URL的限制之后,可传递的信息量及其安全性都显著提高。
1 |
|
此外,PHP中还有一种$_REQUEST数组,其默认包含了$_GET, $_POST,$_COOKIE数组的内容,即其可以访问GET和POST方法以及Cookie传递的数据。但是,$_RESQUET数组的安全性并不可靠,应当谨慎使用。
综上所述,问题456的答案依次为:
Q4:True
Q5:
1 |
|
Q6:$_REQUEST和$_GET
PHP中的表格
Qus 07:
Study the following code:
1 |
|
What will be the output?
a) Error MEssage
b) Jack
Smith
c) Jack Smith
d)
首先是在php中.=
的含义大概相当于+=
其次是这段代码显然将要输出一个表格,但其没有边框线。这是由于HTML表格默认是没有边框的,如果想要添加边框还需要做进一步的修改。
综上所述,这一题的答案为b)
PHP中的数据类型
Which of the following is NOT a supported Data Type in PHP?
a) Resource
b) String
c) Char
d) Boolean
e) Float
事实上,由于PHP是一种弱类型语言,其中并没有专门的char
数据类型。如果要实现类C语言中的char
数据类型的功能,在PHP中可以使用string
数据类型来代替。此外,PHP存在一种比较少见的resource
数据类型,其用来表示外部资源。PHP本身无法直接操作这些资源,但可以通过resource
类型的变量来引用它们。
1 |
|
综上所述,这道题的答案为c)