Blogger Information
Blog 13
fans 0
comment 0
visits 9678
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
循环语句 超级全局变量 及 cURL函数
樱风醉
Original
791 people have browsed it

一、循环语句

1.while 循环

  1. while (条件)
  2. {
  3. 要执行的代码;
  4. }
  1. //示例:1-130每13个数字排一行
  2. $a = 1;
  3. while($a <= 130){
  4. echo $a."\t";
  5. if($a % 13 == 0){
  6. echo '<br>';
  7. }
  8. $a++;
  9. }

2.do…while 循环

do…while 语句会至少执行一次代码,然后检查条件,只要条件成立,就会重复进行循环

  1. do
  2. {
  3. 要执行的代码;
  4. }
  5. while (条件);
  1. //示例:计算0-100的整数和
  2. $b = 0;$c = 0;
  3. do{
  4. $b += $c;
  5. $c++;
  6. }while($c <= 100);
  7. echo $b;
  8. //输出5050

3.for 循环

  1. for (初始值; 条件; 增量)
  2. {
  3. 要执行的代码;
  4. }
  5. 示例1:输出100内所有能被3整除的数
  6. for($d = 1;$d <= 100;$d++){
  7. if($d % 3 == 0){
  8. echo $d;
  9. echo "\t";
  10. }
  11. }

示例二:九九乘法表

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>九九乘法表</title>
  8. <style>
  9. table{
  10. border-collapse: collapse;
  11. }
  12. td{
  13. width: 80px;
  14. border: 1px solid;
  15. text-align: center;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <?php
  21. echo '<table>';
  22. echo '<caption>九九乘法表</caption>';
  23. for($e = 1;$e <= 9;$e++){
  24. echo '<tr>';
  25. for($f = 1;$f <=$e;$f++){
  26. echo '<td>'.$f.'×'.$e.'='.$e * $f.'</td>';
  27. }
  28. echo '</tr>';
  29. }
  30. echo '</table>';
  31. ?>
  32. </body>
  33. </html>

二、超级全局变量

$GLOBALS

引用全局作用域中可用的全部变量,二维数组

$_SERVER

保存关于报头、路径和脚本位置的信息。服务器的环境,会使用里面的下标是时间戳,也可用函数获取时间戳

$_REQUEST

一维数组:包含了$_POST$_GET$_COOKIE

$_POST

广泛用于收集提交 method="post" 的 HTML 表单后的表单数据。不显示传值内容,这样做,更安全

$_GET

可用于收集提交 HTML 表单 method="get"之后的表单数据,也可以收集 URL 中的发送的数据。网址上显示出来,在浏览器的收藏里使用,很方便

$_FILES

文件上传信息

$_ENV

服务器的环境,会使用里面的下标是时间戳,也会有函数获取时间戳

$_COOKIE

用于取回 cookie 的值。setcookie() 函数用于设置 cookie。cookie 常用于识别用户。cookie 是一种服务器留在用户计算机上的小文件。每当同一台计算机通过浏览器请求页面时,这台计算机将会发送 cookie。通过 PHP,您能够创建并取回 cookie 的值。

$_SESSION

存储和取回 session 变量。PHP session 变量用于存储关于用户会话(session)的信息,或者更改用户会话(session)的设置。Session 变量存储单一用户的信息,并且对于应用程序中的所有页面都是可用的。

三、魔术常量

__LINE__:返回文件中的当前行号。也可写成__line__

__FILE__:返回当前文件的绝对路径(包含文件名)

__DIR__:返回当前文件的绝对路径(不包含文件名),等价于 dirname(__FILE__)

__FUNCTION__:返回当前函数(或方法)的名称

__CLASS__:返回当前的类名(包括该类的作用区域或命名空间)

__TRAIT__:返回当前的 trait 名称(包括该 trait 的作用区域或命名空间)

__METHOD__:返回当前的方法名(包括类名)

__NAMESPACE__:返回当前文件的命名空间的名称

四、预定义常量

PHP_VERSION:返回 PHP 的版本

PHP_OS:返回 执行 PHP 解释器的操作系统名称

PHP_EOL: 系统换行符,Windows 是(\r\n),Linux 是(\n),MAC 是(\r

cURL 函数

  1. //curl_init() 初始化一个cURL会话
  2. $ch = curl_init();
  3. //curl_setopt() 设置一个cURL传输选项
  4. //CURLOPT_URL 需要获取的URL地址,也可以在curl_init()函数中设置
  5. curl_setopt($ch,CURLOPT_URL,'http://www.tianqiapi.com/free/week?unescape=1&appid=6422****&appsecret=Hw******&city=武汉');
  6. //CURLOPT_RETURNTRANSFER 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  8. //curl_exec() 执行一个cURL会话
  9. $html = curl_exec($ch);
  10. echo $html;

  1. function weather($url,$data,$is_post){
  2. $ch = curl_init();
  3. if($is_post == 0){
  4. if(!empty($data)){
  5. $url .= '?';
  6. foreach($data as $key => $value){
  7. $url .= $key.'='.$value.'&';
  8. }
  9. }
  10. }
  11. curl_setopt($ch, CURLOPT_URL ,$url);
  12. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); // 在发起连接前等待的时间,如果设置为0,则无限等待。
  13. curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置cURL允许执行的最长秒数。设置超时限制防止死循环
  14. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// 爬取重定向页面
  15. curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer,防止盗链
  16. curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  17. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 要求结果保存到字符串中还是输出到屏幕上
  18. curl_setopt($ch, CURLOPT_USERAGENT, 'Data');// 在HTTP请求中包含一个"User-Agent: "头的字符串。
  19. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // 强制使用 HTTP/1.1
  20. if($is_post == 1){
  21. curl_setopt($ch,CURLOPT_POST,1);
  22. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  23. }
  24. $html = curl_exec($ch);
  25. //curl_error() 返回一个保护当前会话最近一次错误的字符串
  26. if(curl_errno($ch)){
  27. return curl_errno($ch);
  28. }
  29. curl_close($ch);
  30. return $html;
  31. }
  32. $data = [
  33. 'key' =>'****************',
  34. 'city' => '武汉'
  35. ];
  36. $url = 'http://apis.juhe.cn/simpleWeather/query';
  37. echo weather($url,$data,$is_post=1);

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!