pantelis
pantelis

Reputation: 113

php echo question

$this->get_name = mysqli_fetch_assoc(mysqli_query($this->link,"SELECT complete_name FROM student WHERE email='$this->email'"));

mysqli_query($this->link, $this->drop_query) or die(mysql_error());

echo $this->get_name['complete_name'].  " has been marked as Left!";

when i run this i get the following:

"John has been marked as Left!"

but when i run

echo ".$this->get_name['complete_name'] has been marked as Left!";

i get this:

"Array['complete_name'] has been marked as Left!"

aren't they supposed to print the same stuff?

Upvotes: 0

Views: 70

Answers (3)

Joshua Pinter
Joshua Pinter

Reputation: 47561

Whenever I'm echoing anything more complex than something like $complete_name, I don't include it in the quotes:

echo $this->get_name['complete_name'].' has been marked as Left!';

I find the ease of reading the whole quote as a sentence is lost anyways when dealing with objects and arrays, etc. So if you're looking to read the quoted line easily, I might suggest using:

$complete_name = $this->get_name['complete_name'];   # deal with the dirty stuff here 

echo "$complete_name has been marked as left!";      # so this is much easier to read

Upvotes: 0

Kevin Peno
Kevin Peno

Reputation: 9196

If you want to get the array offset use:

{$this->get_name['complete_name']}

Use of the brackets tells PHP to expect the entire containment to be used as the variable if possible.

See PHP's String Parsing for more details. As part of their examples they have this example:

// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";

Upvotes: 1

akiro
akiro

Reputation: 91

PHP:s parser doesnt know where the variable ends in the second case, use {} to encapsulate the variable in the string

echo "{$this->get_name['complete_name']} has been marked as Left!";

Upvotes: 3

Related Questions