Home >Backend Development >PHP Tutorial >How is urlencode() used?
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';
?>
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>
<code><?php $name='A&B C '; $name=urlencode($name); echo 'http://www.cndn.cc/X1.php?name='.$name; ?></code>
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>
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>
//The solution is the problem of semicolons and double quotes mentioned above