AKalair Posted September 11, 2008 Share Posted September 11, 2008 Hi, I've just started learning PHP and wrote a really basic log in script. But when I log in it says Welcome $name rather than admin. I cant see the problem can anyone hekp me? Thanks <?php $name = $_POST['name']; $password = $_POST['password']; if ($name == admin && $password == password) { echo 'Welcome back. $name'; } else { echo 'Bad password or user name'; } ?> Link to comment https://forums.phpfreaks.com/topic/123743-solved-php-variable-not-showing-value-issue/ Share on other sites More sharing options...
GingerRobot Posted September 11, 2008 Share Posted September 11, 2008 Your particular problem is that variables inside single quotes are not parsed. They are treated as literals. Variables inside double quotes are parsed. As an illustration, try running this: <?php $foo = 'bar'; echo '$foo'; //echos $foo echo '<br />'; echo "$foo"; //echos bar echo '<br />'; echo $foo; //echos bar ?> You have a few options to solve your problem. The two most useful are to either replace those single quotes with doubles: echo "Welcome back. $name"; Or to use single quotes and some concatenation (joining, if you prefer): echo 'Welcome back. '.$name; //the . operator is the concatenation operator As to which you chose, it's largely a matter of personal preference. There is a very very slight increase in performance by using single quotes, since the text inside the quotes need not be parsed. For all practical applications, the difference wouldn't be noticeable, however. There is also a further problem with your script in this line: if ($name == admin && $password == password) The strings admin and password should be contained in quotes - all strings should. Unquoted strings are what are called constants. You can read more about those here. The php engine isn't stupid, however. If it finds an undeclared constant, it assumes that you mean to use the string. However, depending on your error reporting level, you could receive a notice. And it's bad practice to not quote your strings. Therefore, the line should be: if ($name == 'admin' && $password == 'password') p.s. Welcome to the forums. Link to comment https://forums.phpfreaks.com/topic/123743-solved-php-variable-not-showing-value-issue/#findComment-638952 Share on other sites More sharing options...
AKalair Posted September 11, 2008 Author Share Posted September 11, 2008 Thanks for the quick and detailed reply, I appreciate it. I can move on now Link to comment https://forums.phpfreaks.com/topic/123743-solved-php-variable-not-showing-value-issue/#findComment-638955 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.