Abhishek Bhardwaj
Abhishek Bhardwaj

Reputation: 242

PHP printing string

I am using var_dump for printing a variable, named $info. Now I need to print message_id as shown below, but the string is not getting print.

object(stdClass)[10]
  public 'date' => string 'Sun, 14 Oct 2012 19:45:26 +0100' (length=31)
  public 'Date' => string 'Sun, 14 Oct 2012 19:45:26 +0100' (length=31)
  public 'subject' => string 'deposit' (length=7)
  public 'Subject' => string 'deposit' (length=7)
  public 'message_id' => string '<CAKvS7sNb0Cb1AaAbCdEFGHerp5UyJuhfkcJj8P+8CZQj4VTGLA@mail.gmail.com>' (length=68)
  public 'toaddress' => string 'sbsa secretaccount <[email protected]>' (length=43)

I'm using this line,

echo $info->message_id;

Also if I try this,

$temp =  '<CAKvS7sNb0Cb1AaAbCdEFGHerp5UyJuhfkcJj8P+8CZQj4VTGLA@mail.gmail.com>';
echo "::$temp::";

Only :::: is getting printed, the $temp variable is ignored? Why?

Upvotes: 1

Views: 250

Answers (3)

janenz00
janenz00

Reputation: 3310

Agree with both @deifwud and @Kolink. Your string should be :

   $temp = htmlspecialchars('<CAKvS7sNb0Cb1AaAbCdEFGHerp5UyJuhfkcJj8P+8CZQj4VTGLA@mail.gmail.com>');
   echo "::$temp::";

That will escape the < and > so that browser don't intreprate them like tag.

Upvotes: 0

billyonecan
billyonecan

Reputation: 20250

You have an object, not an array. Use:

echo $info->message_id;

php.net - Objects

In response to your second point - as the string is encapsulated in < and >, the browser will try and display this as HTML, if you view the source of the page, you will see that your $temp string is in there.

You could use htmlentities() in order to convert the characters to HTML entities:

echo htmlentites($temp);

Upvotes: 4

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324610

In addition to what deifwud said about you having an object and not an array (they aren't interchangeable like they are in JavaScript), you should also note that anything of the form <blah blah blah> is seen by the browser as an HTML tag and is not displayed raw. If you go to View Source you will see it's right there. To avoid this problem, use htmlspecialchars()

Upvotes: 1

Related Questions