学习笔记_25_03_08

学习笔记_25_03_08

  关于VUC上Topic 1 - Reviosn Quiz, Topic 2 - Reviosn QuizTopic 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_format(number,decimals,decimalpoint,separator)
参数 描述
number 必需,用来填充需要被格式化的变量或数字。
decimals 可选,规定保留几位小数
decimalpoint 可选,规定用作小数点的字符串
separator 可选,规定用作千位分隔符的字符串
  综上所述,这道题的答案为a)


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
2
3
<?php
$rent = 5000;

  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
2
3
$name = "world"
echo 'Hello $name!'; //输出:Hello $name!
echo "Hello $name!"; //输出:Hello world!

  因此,在题目中要求我们采用双引号进行输出的时候,为了避免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
2
3
4
foreach($array as $value)
{
code to be executed;
}

  其有以下几种基本用法:

  一、遍历数组的值

1
2
3
4
5
$xvs = array("GCC", "ZBN", "YZY", "JYH", "XF");
foreach($cars as $value)
{
echo "$value<br>";
}

  输出:

1
2
3
4
5
GCC
ZBN
YZY
JYH
XF

  此处的语法和Python中的For循环就很像,可以辅助理解:

1
2
3
xvs = ["GCC", "ZBN", "YZY", "JYH", "XF"]
for xv in xvs:
print(xv)

  二、遍历数组的键值对

1
2
3
4
5
$xvs = array("GCC" => "8", "ZBN" => "8", "YZY" => "8", "JYH" => "8", "XF" => "19");
foreach($xvs as $name => $age)
{
echo "Name=$name, Age=$age";
}

  输出:

1
2
3
4
5
Name=GCC, Age=8
Name=ZBN, Age=8
Name=YZY, Age=8
Name=JYH, Age=8
Name=XF, Age=19

  三、反过来修改数组中的值

  从PHP5开始,可以通过在$value前加一个&来实现修改原数组中的值。

1
2
3
4
5
6
7
8
9
$array = array(1, 2, 3, 4, 5);
foreach($array as &$value)
{
$value *= 2;
}
foreach($array as $value)
{
echo "$value ";
}

  输出:

1
2 4 6 8 10

  综上所述,答案选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
2
3
4
5
6
7
8
9
10
11
12
13
<?php
if($_GET['get'])
{
echo "$_GET['id']<br>";
echo "$_GET['password']<br>";
}
?>
<form action="get.php"> // action选项中填入get.php意味着这份表单将被发送到此文件中进行处理。
// 此外,form表单中的method变量默认为get
<label for="id">账号:</label><input type="text" name="id"> // for属性规定标签绑定到哪个表单元素
<label for="password">密码:</label><input type="text" name="password">
<input type="submit" name="get" value="提交">
</form>

  和$_GET相对的,PHP中还有$_POST的用法。前者通常用于从服务器中检索信息,而后者则是用于向服务器中发送数据。特别要注意的是,POST请求会把数据放在HTTP请求的主体当中,故其传递的信息不会在URL中显示。在没有了URL的限制之后,可传递的信息量及其安全性都显著提高。

1
2
3
4
5
6
7
8
9
10
<?php
if($_POST['post']) {
foreach($_POST as $index = > $value) echo "$_POST[$index] = $value", "<BR>";
}
?>
<form action="post.php" method="post"> // 在这里明确了要采用POST的方法
<label for="id">账号:</label><input type="text" name="id">
<label for="password">密码:</label><input type="text" name="password">
<input type="submit" name="post" value="提交">
</form>

  此外,PHP中还有一种$_REQUEST数组,其默认包含了$_GET, $_POST,$_COOKIE数组的内容,即其可以访问GET和POST方法以及Cookie传递的数据。但是,$_RESQUET数组的安全性并不可靠,应当谨慎使用。

  综上所述,问题456的答案依次为:

  Q4:True

  Q5:

1
2
3
$first_name = $_POST['fname'];
$last_name = $_POST['lname'];
$title = $_POST['title'];

  Q6:$_REQUEST和$_GET



PHP中的表格

  Qus 07:
  Study the following code:

1
2
3
4
5
6
<?php
$html = "<table>";
$html .= "<tr><td>Jack</td></tr>";
$html .= "<tr><td>Smith</td></tr>";
$html 》= "</table>";
echo $html;

  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
2
3
$file = fopen("example.txt", "r"); // $file是一个文件资源
$conn = mysqli_connect("localhost", "user", "password", "database"); // coon是一个数据库连接资源
// .......

  综上所述,这道题的答案为c)


学习笔记_25_03_08
http://example.com/2025/03/08/note03/
作者
谢斐
发布于
2025年3月8日
许可协议