Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/139066-solved-variable-problem-any-ideas/
Share on other sites

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.