3

I have made a small multiplication function

 <p><?php
 $a= "<?php echo $this->prodDet->v_price?>";
 $b=.26;
 $c=$a*$b;
 echo $c;
 ?>

Here the price value is extracted from database of the requiste product and is multiple by fixed .26 variable. Wondering why am getting this errror -

Catchable fatal error: Object of class stdClass could not be converted to string in

I am learning the basics of PHP. can someone suggest how to get it solved please?

Thanks

Ben
  • 54,723
  • 49
  • 178
  • 224
Ruchika
  • 503
  • 1
  • 8
  • 26
  • 4
    When you're writing PHP, bear in mind that you can never nest `` blocks. When you get to `$a="`), so that's where things stop making sense. You're trying to set $a to the string `prodDet->v_price?>`, which is valid, but not what you want to do. As a side-effect it's trying to evaluate `$this` as a string because you're using [double-quotes](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php). – Matt Gibson Oct 13 '13 at 07:29

4 Answers4

4

You don't know what you're trying here?

$a = "<?php echo $this->prodDet->v_price?>";

$a looks after the assignment like <?php echo <the value of $this->prodDet->v_price>?>. The <?php part there is not executed. You surely want to write:

$a = $this->prodDet->v_price;

And make sure that $a is not an object at the end! (what you can check via var_dump($a); in the line after the assignment)

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
bwoebi
  • 23,637
  • 5
  • 58
  • 79
3

v_price has been set as a class instance somewhere before these lines of code. My guess is that happens somewhere in prodDet.

So, when you echo it, PHP tries to convert to a string and fails.

To see what kind of class it is, try:

var_dump($this->prodDet->v_price);

That will help you debug.


An example is in PHP: Catchable fatal error: Object of class stdClass could not be converted to string, where the person accidentally sets a variable to be a class in this line:

$procID = $client->start(array("prefix"=>"Genesis"));

then they try to echo $procID and get the same error. You can't echo a class because it can't be changed to a string.


Also, when you do

$a = echo ...

$a will not be what you expect, since echo just prints out to the screen! It doesn't return the value. You'll want to remove the echo and just set it directly:

$a = $this->prodDet->v_price->getSomeValue();
Community
  • 1
  • 1
Ben
  • 54,723
  • 49
  • 178
  • 224
3

I also get same error message which is

enter image description here

after one hour checking everything i find that i have done some small mistake which is i have use variable sign ($) twice when fetching data. which is

enter image description here

After remove one dollar sign it's working fine,it may be help someone:)

user3728517
  • 157
  • 3
  • 10
0

With primitive variable and strings, $name = "Charlie";

Objects store many, related things together. Strings, numbers, booleans, etc are stored as properties of the object. Properties are accessed with the object operator ("arrow operator") like $person->name = "Charlie";

Andrew Koper
  • 6,481
  • 6
  • 42
  • 50