Chappers Posted January 1, 2009 Share Posted January 1, 2009 Hi, just a quickie. This works, so I can get as many error/success messages as I want during a script, then have them all print out wherever I want them to: <?php if (empty($example)) { $msgs[] = "Example did not work";} else { $msgs[] = "Example worked";} if (empty($another)) { $msgs[] = "Another did not work";} else { $msgs[] = "Another worked";} foreach ($msgs as $message) { echo "$message<br>";} So in a certain section I get all messages printed one after another. No problem. But if I need to add the name of a variable which was involved to $msgs[], so I know exactly what it's referring to, like this: <?php if (empty($example)) { $msgs[] = "Example did not work";} else { $msgs[] = "Example worked";} if (empty($another)) { $msgs[] = "$another did not work";} else { $msgs[] = "$another worked";} foreach ($msgs as $message) { echo "$message<br>";} The code fails with an error regarding the square brackets. Is there a way I can do it this way but have $msgs[] = "Blah blah $variable blah blah"; and have it work? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/139066-solved-variable-problem-any-ideas/ Share on other sites More sharing options...
xtopolis Posted January 1, 2009 Share Posted January 1, 2009 Use single quotes when you don't want the variable name to be interpreted. Quote Link to comment https://forums.phpfreaks.com/topic/139066-solved-variable-problem-any-ideas/#findComment-727328 Share on other sites More sharing options...
Chappers Posted January 1, 2009 Author Share Posted January 1, 2009 Oh, I do want the variable name to be interpreted. I want $another from my example to be entered into $msgs[] as the string it originally was, but either way, it produces an error. I've tried: <?php $msgs[] = "This $example failed"; $msgs[] = "This ".$example." failed"; $msgs[] = "This '$example' failed"; $msgs[] = "This {$example} failed"; $msgs[] = $example; ?> And all create a parse error: Parse error: parse error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING Quote Link to comment https://forums.phpfreaks.com/topic/139066-solved-variable-problem-any-ideas/#findComment-727330 Share on other sites More sharing options...
xtopolis Posted January 1, 2009 Share Posted January 1, 2009 $msgs[] = 'This $example succeeded.'; Quote Link to comment https://forums.phpfreaks.com/topic/139066-solved-variable-problem-any-ideas/#findComment-727334 Share on other sites More sharing options...
.josh Posted January 1, 2009 Share Posted January 1, 2009 none of those should have failed. Your error is somewhere else. Quote Link to comment https://forums.phpfreaks.com/topic/139066-solved-variable-problem-any-ideas/#findComment-727336 Share on other sites More sharing options...
Chappers Posted January 1, 2009 Author Share Posted January 1, 2009 You are, of course, absolutely right - the problem was elsewhere. The syntax checker I use online was correctly finding an issue, but wrongly stating what the issue was, as it sometimes does (but it's good overall to find errors). I would have realised if perhaps I hadn't been doing this at 3am... For others who might search and find this I'll explain: I was using this snippet of code from a much larger page: <?php $result = mysql_query("DROP TABLE $table"); if (mysql_error()) { $errmsg = "<span class='error'>Query failed.<br>MySQL Error Number: ".mysql_errno()."<br>MySQL Error Description: ".mysql_error()."</span>"; echo $errmsg; } else { echo "Table $table deleted"; } ?> That worked fine, until I changed it to use an array so that I can print all error/success messages afterwards on a specific area of the page. I removed the <span class...> from the error message as I could add a class to the whole contents of the $msgs[] array myself before printing it. My test code was: <?php $result = mysql_query("DROP TABLE $table"); if (mysql_error()) { $msgs[] = "Query failed.<br>MySQL Error Number: ".mysql_errno()."<br>MySQL Error Description: ".mysql_error()."; } else { $msgs[] = "Table $table deleted"; } foreach ($msgs as $message) { echo $message; }?> It was the code above that wasn't working, creating an error wrongly attributed to the $msgs[] square bracket. It was my lack of understanding about the correct format to use when mixing normal words and functions, if that's how to describe it. It was this: <?php // Below is the incorrect line, only one double-quotes at the end of it. $msgs[] = "Error inserting data.<br>MySQL Error Number: ".mysql_errno()."<br>MySQL Error Description: ".mysql_error()."; // This one works $msgs[] = "Error inserting data.<br>MySQL Error Number: ".mysql_errno()."<br>MySQL Error Description: ".mysql_error().""; ?> So, trouble started when I removed the span class attribute because I didn't realise that when I removed the </span> at the end, I needed to leave the extra set of double quotes. I still don't properly get the business of functions and words mixed together and using "." to switch between the two, but it's working now anyway. Quote Link to comment https://forums.phpfreaks.com/topic/139066-solved-variable-problem-any-ideas/#findComment-727496 Share on other sites More sharing options...
xtopolis Posted January 1, 2009 Share Posted January 1, 2009 http://us.php.net/types.string The jist of it is: Single quotes are straight 'display what is there, but do not try to change anything' echo 'I like php.'; // I like php echo 'I like $php.'; // I like $php Double quotes are for interpretations, where it tries to "find the value" $php = 'cats'; echo "I like php."; // I like php echo "I like $php."; // I like cats the "." (dot) operator is used for concatenation - the joining of strings $str = 'I like '; $str2 = 'elephants.' echo $str . $str2; // I like elephants. Quote Link to comment https://forums.phpfreaks.com/topic/139066-solved-variable-problem-any-ideas/#findComment-727517 Share on other sites More sharing options...
Chappers Posted January 1, 2009 Author Share Posted January 1, 2009 That's very helpful, thanks a lot. I didn't know those things, so it'll help me understand why my code sometimes does unexpected things. Quote Link to comment https://forums.phpfreaks.com/topic/139066-solved-variable-problem-any-ideas/#findComment-727563 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.