Home > Backend Development > PHP Tutorial > How is urlencode() used?

How is urlencode() used?

WBOY
Release: 2016-07-06 13:52:57
Original
1705 people have browsed it

I use it like this, but it doesn’t seem to work. It says $name in the fourth line is undefined or something
$name='A&B C '
$name=urlencode($name) ;
echo 'http://www.cndn.cc/X1.php?name=$name';
?>

Reply content:

I use it like this, but it doesn’t seem to work. It says $name in the fourth line is undefined or something
$name='A&B C '
$name=urlencode($name) ;
echo 'http://www.cndn.cc/X1.php?name=$name';
?>

1.$name='A&B C ' // Less semicolons
2. To output variables in a string, double quotes are required

The following is the correct code:

<code>$name='A&B C ';
$name=urlencode($name);
echo "http://www.cndn.cc/X1.php?name=$name";</code>
Copy after login

<code><?php
$name='A&B C ';
$name=urlencode($name);
echo 'http://www.cndn.cc/X1.php?name='.$name;
?></code>
Copy after login

Dude, you are missing a semicolon in the second line.

Furthermore, you want to output the value of $name in the last line, so you cannot use single quotes, you have to use double quotes.

<code><?php
$name='A&B C ';  //你太粗心大意了,这里的分号漏掉了!
$name=urlencode($name);
echo 'http://www.cndn.cc/X1.php?name=' . $name; //单引号中的变量是不会被解析的,要么用双引号,要么用这种方式
?></code>
Copy after login

Pay more attention to grammar when writing code in the future. You should not make such low-level mistakes.

<code>$name='A&B C ';
$name=urlencode($name);
echo "http://www.cndn.cc/X1.php?name={$name}";
echo "http://www.cndn.cc/X1.php?name=$name"; //个人不建议这种
echo 'http://www.cndn.cc/X1.php?name=' . $name;</code>
Copy after login

//The solution is the problem of semicolons and double quotes mentioned above

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template