0

I am currently making a hangman game with php and am trying to print out the blank lines using:

$hidden = $_SESSION['hidden']; foreach ($hidden as $character){ echo $character." "; } 

However, I am getting an error - Invalid argument supplied for foreach()

I was wondering what I did wrong with my syntax?

4
  • Do you use session_start() and how do you set $_SESSION['hidden']? Commented May 21, 2018 at 14:39
  • What php version are you running? Commented May 21, 2018 at 14:40
  • 1
    You can't iterate with foreach over a string. Use for. Commented May 21, 2018 at 14:41
  • 1
    Easier echo implode(' ', str_split($hidden)); Commented May 21, 2018 at 15:18

1 Answer 1

1

You have to have an array to use foreach(), you could use for() (loop along length of string) or simply use str_split() to convert the string to an array...

$hidden = $_SESSION['hidden']; foreach (str_split($hidden) as $character){ echo $character." "; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.