Can anyone help?

This code is from PHP & MySQL in easy steps. and for some reason it allegedly works for Mike McGrath but not for me, I have spent three days retyping this, checking over and over again to make sure the code is not full of errors and I am starting to think Mike McGrath is full of it. The error I am getting is caused by Step 4 below apparently and I cannot resolve it. I am starting to despise php. Also trying to use preformatted text to share my code and I don’t think it’s working and neither is the tags.

# Step 4
echo "<h1>HOME</h1>
<p> You are now logged in, { $_SESSION[ 'first_name' ] } { $_SESSION[ 'last_name' ] } </p>" ;

So which one? What is the specific error message? At least you could try some online editor like

https://3v4l.org/5B9sV

and have a look at the manual

https://www.php.net/manual/en/language.types.string.php

see " Complex (curly) syntax"

Apologies, the error I am getting is Parse error : syntax error, unexpected ‘’ (T_ENCAPSED_AND_WHITESPACE), expecting ‘-’ or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\Abyss Web Server\htdocs\home.php on line 26

And what did you learn from the manual? Did you really follow the rules precisely? I’ll quote that (and i don’t have a clue what it’s in your book)

Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }

AS chorn says and without spaces!

1 Like

I have tried this code every which way I can, even deleted the file and rewrote the pages three times.

I will be honest, this is wrecking my head lol.

Okay i dont have a clue either. I never use that curly syntax because i do always paste my strings together with the concatenation operator so that i never have to place my variables into the text.
In this case we could get something like this:

echo '<h1>HOME</h1><p> You are now logged in, ' . $_SESSION['first_name']  . ' ' . $_SESSION['last_name']  . '</p>' ;

just copy and paste it in your editor. The concatenation operator (.) glues anything together. Watch the syntax highlighting.

The problem is all the spaces you have in the variables.

Tried it, but it doesn’t work. I have gotten as far as putting details into the login page then instead of loading the home page it just refreshes the login page asking me to login again. honestly, I am getting to the end of my patience with PHP. Starting to REALLY not like it!

If the php syntax error has been corrected, then doing what was suggested did work and the current problem is due to something else. (As a side note, if you put php variables inside a double-quoted string, when php parses the string it produces the same tokenized byte-code as if you had used concatenation, without needing to type the extra quotes and dots yourself.)

It would take seeing all the code needed to reproduce this new problem to help narrow down the dozen different things that could be wrong with it, to just a few or a single thing that can then be investigated further. Except for very overt problems and errors, there’s not a ‘one symptom’ is always caused by a ‘single problem’ relationship in programming.

As to the initial problem in this thread. The book publisher used full-width justification to make the longer lines fill the width of the page. This however introduced spaces in the ‘published’ code where they are not functionally allowed. Duplicating what you see in a book or in a video isn’t actually teaching you the requirements/meaning of what you are doing, so when it comes to finding and fixing a problem or writing your own code, you don’t have any idea what is actually required. This is where the documentation comes in handy. The link that chorn posted to in the php.net documentation specifically states that this syntax requires the $ to immediately follow the { for it to work.

Sponsor our Newsletter | Privacy Policy | Terms of Service