很多站长朋友们都不太清楚php参数,今天小编就来给大家整理php参数,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 PHP接受命令参数 2、 Php获取参数的几种方法 3、 php如何用标签传递参数?传递后如何接受该参数? 4、 php如何传递类参数 5、 php函数设定参数类型 PHP接受命令参数通常PHP都做http方式请求了,可以使用GET or
POST方式接收参数,有些时候需要在shell命令下把PHP当作脚本执行,比如定时任务。这就涉及到在shell命令下如何给php传参的问题,通常有三种方式传参。
一、使用$argv
or $argc参数接收
复制代码
代码如下:
<?php
/**
* 使用 $argc $argv
接受参数
*/
echo "接收到{$argc}个参数";
print_r($argv);
执行
复制代码
代码如下:
[root@DELL113 lee]# /usr/local/php/bin/php
test.php
接收到1个参数Array
(
[0] => test.php
)
[root@DELL113
lee]# /usr/local/php/bin/php test.php a b c d
接收到5个参数Array
(
[0]
=> test.php
[1] => a
[2] => b
[3] => c
[4] => d
)
[root@DELL113
lee]#
二、使用getopt函数
复制代码
代码如下:
<?php
/**
* 使用
getopt函数
*/
$param_arr =
getopt('a:b:');
print_r($param_arr);
执行
复制代码
代码如下:
[root@DELL113 lee]# /usr/local/php/bin/php
test.php -a 345
Array
(
[a] => 345
)
[root@DELL113 lee]#
/usr/local/php/bin/php test.php -a 345 -b 12q3
Array
(
[a] =>
345
[b] => 12q3
)
[root@DELL113 lee]# /usr/local/php/bin/php
test.php -a 345 -b 12q3 -e 3322ff
Array
(
[a] => 345
[b]
=> 12q3
)
三、提示用户输入
复制代码
代码如下:
<?php
/**
*
提示用户输入,类似Python
*/
fwrite(STDOUT,'请输入您的博客名:');
echo
'您输入的信息是:'.fgets(STDIN);
Php获取参数的几种方法1.获取地址栏参数:
$_SERVER['QUERY_STRING'];
2.获取参数值
//post提交
用$_POST['参数名称']获取值
//get 提交
用$_GET['参数名称']获取值
//无论是post还是get方式提交都可以用$_REQUEST
用$_REQUEST['参数名称']获取值
php如何用标签传递参数?传递后如何接受该参数?写法如下:
<a href='deal.php?id=5' >
在deal.php里面:
用$_GET['id']来获取
<?php
$result = $_GET["id"];
echo $result;
?>
PHP,是英文超文本预处理语言Hypertext Preprocessor的递归缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛地运用。可以生成Forms,ComboBoxes,Grid,Menus等的组件,并支持将数据转为XML/JSON格式。
PHP类中,可能有多个属性参数。当使用new创建一个对象的时候,可能需要完成初始化操作,需要从外边传递参数进来。
PHP通过引用传递参数用法的示例:
<?php
function add_some_extra($string) // 引入变量,使用同一个存储地址
{
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // outputs 'This is a string, and something extra.'
?>
输出:
This is a string, and something extra.
如果没有这个符号,
<?php
function add_some_extra($string)
{
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // outputs 'This is a string, '
?>
输出:
This is a string,
php如何传递类参数PHP类中,可能有多个属性参数。当使用new创建一个对象的时候,可能需要完成初始化操作,需要从外边传递参数进来。下面演示具体过程:
?php
class Test {
//定义私有变量name ,age
private $name, $age;
//构造函数,初始化的时候最先执行
public function __construct($name, $age) {
$this-
name = $name;
$this->age = $age;
}
public function showMsg() {
return "大家好,我叫".$this->name.";今年".$this->age."岁了!";
}
}
//定义参数
$name="百度知道";
$age=10;
//初始化类的时候传递参数
$te=new Test($name, $age);
echo $te->showMsg();
//输出结果:大家好,我叫百度知道;今年10岁了!
?>
php函数设定参数类型php 函数的参数类型可以指定为类名或数组类型array,比如
这样是对的public function Right( My_Class $a, array $b )
这样是错的public function Wrong( string $a, boolean $b )
如果需要其他类型,需要在函数内部进行类型检查
参考
这一段
public function Right( My_Class $a, array $b )
tells first argument have to by object of My_Class, second an array. My_Class means that you can pass also object of class that either extends My_Class or implements (if My_Class is abstract class) My_Class. If you need exactly My_Class you need to either make it final, or add some code to check what $a really.
Also note, that (unfortunately) "array" is the only built-in type you can use in signature. Any other types i.e.:
public function Wrong( string $a, boolean $b )
will cause an error, because PHP will complain that $a is not an *object* of class string (and $b is not an object of class boolean).
So if you need to know if $a is a string or $b bool, you need to write some code in your function body and i.e. throw exception if you detect type mismatch (or you can try to cast if it's doable).
关于php参数的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。