PHP echo $_SERVER['PHP_SELF'] with added v

2020-07-13 10:12发布

Just want to do $_SERVER['PHP_SELF'] as a link with ?logout=1 appended to it.

<a href="<?php echo ''$_SERVER['PHP_SELF']'.logout=1' ?>" id="add"><input type="button" value="LOGOUT" /></a>

gives

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in F:\export\srv\www\vhosts\main\htdocs\php\assign3\m_a2_functions.php on line 90

标签: php
6条回答
手持菜刀,她持情操
2楼-- · 2020-07-13 10:36

Change:

<?php echo ''$_SERVER['PHP_SELF']'.logout=1' ?>

To:

<?php echo $_SERVER['PHP_SELF'], '?logout=1' ?>
查看更多
走好不送
3楼-- · 2020-07-13 10:39

This may be a place where printf() would come in handy.

<a href="<?php printf( '%s?logout=1', $_SERVER['PHP_SELF'] ); ?>">Foo</a>

This cuts down on the number of times we hop in and out of a string.

查看更多
来,给爷笑一个
4楼-- · 2020-07-13 10:42

have you tried:

<?php echo($_SERVER['PHP_SELF'] . "?logout=1") ?>
查看更多
何必那么认真
5楼-- · 2020-07-13 10:49

Your quotes are all over the place:

<a href="<?php echo $_SERVER['PHP_SELF'] . '?logout=1'; ?>" id="add"><input type="button" value="LOGOUT" /></a>
查看更多
SAY GOODBYE
6楼-- · 2020-07-13 10:59
<a href="<?php echo $_SERVER['PHP_SELF'] . '?logout=1' ?>" id="add">
查看更多
SAY GOODBYE
7楼-- · 2020-07-13 11:03

You are defining an anchor (<a>) and putting a button inside it, but the button will not send you to that url. You should make the "onclick" event of the button redirect the user to the page you want.

Also, if you just want to add a variable to the current URL you don't really need to use PHP_SELF, the browser will know what to do.

<input type="button" value="LOGOUT" onclick="window.location.href='?logout=1';"/>
查看更多
登录 后发表回答