ankur0101 Posted May 2, 2012 Share Posted May 2, 2012 Hi, I have following code : <?php class CategoryWork { public $form = ""; public $error = ""; public $add_form = "<br /><br /><p><strong>Add New Category</strong></p><br /><form id=\"form1\" name=\"form1\" method=\"post\" action=\"category.php?action=add\"> <table width=\"550\" height=\"170\" border=\"0\"> <tr> <td width=\"153\">Name :</td> <td colspan=\"2\"><label for=\"cat_name\"></label> <input name=\"cat_name\" type=\"text\" id=\"cat_name\" size=\"50\" maxlength=\"50\" /></td> </tr> <tr> <td>Slug :</td> <td colspan=\"2\"><label for=\"cat_slug\"></label> <input name=\"cat_slug\" type=\"text\" id=\"cat_slug\" size=\"50\" maxlength=\"50\" /></td> </tr> <tr> <td>Description</td> <td colspan=\"2\"><label for=\"cat_desc\"></label> <textarea name=\"cat_desc\" id=\"cat_desc\" cols=\"48\" rows=\"10\"></textarea></td> </tr> <tr> <td> </td> <td width=\"97\"><input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" /></td> <td width=\"286\"><input type=\"reset\" name=\"button2\" id=\"button2\" value=\"Reset\" /></td> </tr> <tr> <td> </td> <td colspan=\"2\">error</td> </tr> </table> </form>"; } ?> At the line of <td colspan=\"2\">error</td> I want to insert $error, hence I made the line as <td colspan=\"2\">".$error."</td> and also tried <td colspan=\"2\">".$this->error."</td> After executing with this, php says syntax error at the line of public $add_form I am going to declare an error at the $error When I do <td colspan=\"2\">$error</td> it says : Parse error: syntax error, unexpected '"' at line of public $add_form When I do <td colspan=\"2\">".$error."</td> it says : Parse error: syntax error, unexpected '.', expecting ',' or ';' at the above line i.e. of <td> I am damn confused, I googled but nothing found any helpful material. Anybody please help me.. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/261950-use-one-variable-value-in-another-variable-under-same-class/ Share on other sites More sharing options...
Zephni Posted May 2, 2012 Share Posted May 2, 2012 <?php class CategoryWork { public $form = ""; public $error = ""; public $add_form = "<br /><br /><p><strong>Add New Category</strong></p><br /><form id='form1' name='form1' method='post' action='category.php?action=add'> <table width='550' height='170' border='0'> <tr> <td width='153'>Name :</td> <td colspan='2'><label for='cat_name'></label> <input name='cat_name' type='text' id='cat_name' size='50' maxlength='50' /></td> </tr> <tr> <td>Slug :</td> <td colspan='2'><label for='cat_slug'></label> <input name='cat_slug' type='text' id='cat_slug' size='50' maxlength='50' /></td> </tr> <tr> <td>Description</td> <td colspan='2'><label for='cat_desc'></label> <textarea name='cat_desc' id='cat_desc' cols='48' rows='10'></textarea></td> </tr> <tr> <td> </td> <td width='97'><input type='submit' name='button' id='button' value='Submit' /></td> <td width='286'><input type='reset' name='button2' id='button2' value='Reset' /></td> </tr> <tr> <td> </td> <td colspan='2'>$this->error</td> </tr> </table> </form>"; } ?> Just replaced all \" with ', See if you still get the php error. EDIT: I just used the class and didn't get the error Quote Link to comment https://forums.phpfreaks.com/topic/261950-use-one-variable-value-in-another-variable-under-same-class/#findComment-1342265 Share on other sites More sharing options...
ankur0101 Posted May 2, 2012 Author Share Posted May 2, 2012 I think PHP cannot store the form content with what you have written. I copy and pasted your $add_form content and PHP shows errors at that line only. Quote Link to comment https://forums.phpfreaks.com/topic/261950-use-one-variable-value-in-another-variable-under-same-class/#findComment-1342268 Share on other sites More sharing options...
Zephni Posted May 2, 2012 Share Posted May 2, 2012 Sorry, I just updated that part, copy out the errors line Quote Link to comment https://forums.phpfreaks.com/topic/261950-use-one-variable-value-in-another-variable-under-same-class/#findComment-1342269 Share on other sites More sharing options...
freelance84 Posted May 2, 2012 Share Posted May 2, 2012 Alternatively: public $add_form = <<<_END <br /><br / the rest of your form but without escping characters as in heredoc var _END; Putting all the form in a heredoc var will mean you can use double or single without having to worry about escaping anything. Quote Link to comment https://forums.phpfreaks.com/topic/261950-use-one-variable-value-in-another-variable-under-same-class/#findComment-1342270 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2012 Share Posted May 2, 2012 The problem is that you cannot use variable values or even the dot concatenation operator in the declaration of your class properties. You can only assign fixed/constant values when you declare a class property. To do what you want, you will need to assign that string to the $add_form property at runtime using code. Quote Link to comment https://forums.phpfreaks.com/topic/261950-use-one-variable-value-in-another-variable-under-same-class/#findComment-1342278 Share on other sites More sharing options...
ankur0101 Posted May 2, 2012 Author Share Posted May 2, 2012 The problem is that you cannot use variable values or even the dot concatenation operator in the declaration of your class properties. You can only assign fixed/constant values when you declare a class property. To do what you want, you will need to assign that string to the $add_form property at runtime using code. Can you give me example of this ? Quote Link to comment https://forums.phpfreaks.com/topic/261950-use-one-variable-value-in-another-variable-under-same-class/#findComment-1342311 Share on other sites More sharing options...
rythemton Posted May 2, 2012 Share Posted May 2, 2012 You'll have to add that to the constructor: class ThisIsMyClass { public $var1 = ''; public $var2 = ''; function __construct( $newvar ) { $this->var1 = $newvar; $this->var2 = 'This is my text being concatinated ' . $this->var1 . ' with other text.'; } } Hope this sample code helps. Quote Link to comment https://forums.phpfreaks.com/topic/261950-use-one-variable-value-in-another-variable-under-same-class/#findComment-1342350 Share on other sites More sharing options...
ankur0101 Posted May 3, 2012 Author Share Posted May 3, 2012 By some how methode, I did what I wanted. I declared error variable before add form variable separately and now no problems Quote Link to comment https://forums.phpfreaks.com/topic/261950-use-one-variable-value-in-another-variable-under-same-class/#findComment-1342539 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.