zenix Posted October 30, 2008 Share Posted October 30, 2008 Hi, I am learning this and doing alright (well...until now), I have hit a wall that seems to define logic. If someone could help me out I would REALLY appreciate it. The code is below, the error I get is "Undefined variable $movie_details. The first instance of the variable works well, but the second one comes up undefined. The table I am attempting to display does show, but so does the error in the upper left of the screen.Thanks a lot in advance! Sorry my coding isn't very efficient, I will improve in time right now I am just trying to learn. while($row = mysql_fetch_array($result)) { $movie_name = $row['movie_name']; $movie_director = $row['movie_director']; $movie_leadactor = $row['movie_leadactor']; $movie_details .=<<<EOD <tr> <td>$movie_name</td> <td>$movie_director</td> <td>$movie_leadactor</td> </tr> EOD; } $movie_details .=<<<EOD <tr> <td> </td> </tr> <tr> <td>Total :$num_movies Movies</td> </tr> EOD; Quote Link to comment https://forums.phpfreaks.com/topic/130759-undefined-variable/ Share on other sites More sharing options...
Maq Posted October 30, 2008 Share Posted October 30, 2008 You need to initialize it first. Before your while loop do: $movie_details = ""; Quote Link to comment https://forums.phpfreaks.com/topic/130759-undefined-variable/#findComment-678611 Share on other sites More sharing options...
revraz Posted October 30, 2008 Share Posted October 30, 2008 Your first variable doesn't need the period, since you are not appending it. $movie_details =<<<EOD Quote Link to comment https://forums.phpfreaks.com/topic/130759-undefined-variable/#findComment-678695 Share on other sites More sharing options...
Maq Posted October 30, 2008 Share Posted October 30, 2008 Your first variable doesn't need the period, since you are not appending it. $movie_details = You are if you initialize it It's also better practice to initialize variables. Quote Link to comment https://forums.phpfreaks.com/topic/130759-undefined-variable/#findComment-678700 Share on other sites More sharing options...
revraz Posted October 30, 2008 Share Posted October 30, 2008 He would be initializing it when he puts data in it. Quote Link to comment https://forums.phpfreaks.com/topic/130759-undefined-variable/#findComment-678718 Share on other sites More sharing options...
Maq Posted October 30, 2008 Share Posted October 30, 2008 Yes I know, but there is a while loop before it. So if there's nothing in the loop then menu_details will never be initialized and if he tried to use that variable in another section he would still receive that same error. I come from a java background, so I'm used to initializing and assigning types to everything Even though, I still don't follow some of these practices in PHP... Quote Link to comment https://forums.phpfreaks.com/topic/130759-undefined-variable/#findComment-678723 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.