Append variable at end of URL - PHP

末鹿安然 提交于 2019-12-23 09:59:43

问题


I want to append the variable $name to the URL http://google.com/script.php so that I can call the variable in another script.

How do I modify the following code without having those syntax errors?

Thanks.

Codes:

$name = $_GET['smsname'];
$call = $client->account->calls->create('+103632', $number,                                             
        'http://google.com/script.php'
    );

回答1:


Append it like so

$name = $_GET['smsname'];
$call = $client->account->calls->create('+103632', $number,                                             
        'http://google.com/script.php?name='.$name
    );

And in the next page you can get it using $_GET['name']




回答2:


Just concatenate it using .

$var1 = 'text'; $var2 = 'text2';

Concatenation = $var1.$var2;

In your case.

$name = $_GET['smsname'];
$call = $client->account->calls->create('+103632', $number,                                             
        'http://google.com/script.php?name='.$name;
    );

But, you should validate $name for values, as in if it is empty or not. Because, that code of yours will only works if if script.php?name= has a value, but if it is not, then you should be prepared to do something instead. like this:

ex: $name = isset($_GET['smsname']) ? $_GET['smsname'] : 0;

The above code pretty much says, if script.php?name= is set to some value, then assign it to $name else $name should be 0

The : 0; part, is there to set a value of 0 IF nothing is set or if $_GET['smsname'] is empty. You can add anything you like there



来源:https://stackoverflow.com/questions/16174262/append-variable-at-end-of-url-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!