GET URL parameter in PHP(获取 PHP 中的 URL 参数)
问题描述
我正在尝试将 URL 作为 php 中的 url 参数传递,但是当我尝试获取此参数时,我什么也没得到 p>
我正在使用以下网址形式:
http://localhost/dispatch.php?link=www.google.com我正在努力解决:
$_GET['link'];但没有返回.有什么问题?
$_GET 不是函数或语言结构——它只是一个变量(一个数组).试试:
特别是,它是一个superglobal:一个内置的在由 PHP 填充的变量中,并且在所有范围内都可用(您可以在没有 全局关键字).
由于变量可能不存在,您可以(并且应该)确保您的代码不会触发通知:
或者,如果您想跳过手动索引检查并可能添加进一步的验证,您可以使用 filter 扩展:
最后但同样重要的是,您可以使用 空合并运算符(从 PHP/7.0) 处理丢失的参数:
echo $_GET['link'] ??'后备价值';I'm trying to pass a URL as a url parameter in php but when I try to get this parameter I get nothing
I'm using the following url form:
http://localhost/dispatch.php?link=www.google.com
I'm trying to get it through:
$_GET['link'];
But nothing returned. What is the problem?
$_GET is not a function or language construct—it's just a variable (an array). Try:
<?php
echo $_GET['link'];
In particular, it's a superglobal: a built-in variable that's populated by PHP and is available in all scopes (you can use it from inside a function without the global keyword).
Since the variable might not exist, you could (and should) ensure your code does not trigger notices with:
<?php
if (isset($_GET['link'])) {
echo $_GET['link'];
} else {
// Fallback behaviour goes here
}
Alternatively, if you want to skip manual index checks and maybe add further validations you can use the filter extension:
<?php
echo filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL);
Last but not least, you can use the null coalescing operator (available since PHP/7.0) to handle missing parameters:
echo $_GET['link'] ?? 'Fallback value';
这篇关于获取 PHP 中的 URL 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取 PHP 中的 URL 参数
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
