aileia Posted February 12, 2008 Share Posted February 12, 2008 I am using drupal 4.7x. I write the following code in the blog entry <?php $life=42; function meaningOfLife() { global $life; print "The meaning of life is $life<br />"; } meaningOfLife(); ?> and then select php code as input format. The result turn out to be The meaning of life is which is wrong, apparently, what i want is The meaning of life is 42. This is an example given in php.net so it should be correct. Moreover since the function is executed, it confirms I installed php. However I can't define the variable $life in the function. It is printing the text, so it is calling the function however it is not calling the variable. Can anyone tell me what went wrong? Quote Link to comment Share on other sites More sharing options...
Cognito Posted February 12, 2008 Share Posted February 12, 2008 try and echo the line rather then print it? Quote Link to comment Share on other sites More sharing options...
aileia Posted February 12, 2008 Author Share Posted February 12, 2008 Hi Cognito, I tried it with echo, it gives the same thing, I can't define the variable in the function. Quote Link to comment Share on other sites More sharing options...
Cognito Posted February 12, 2008 Share Posted February 12, 2008 My bad noticed another mistake - replace this $life=42; with this $life = '42'; Quote Link to comment Share on other sites More sharing options...
aileia Posted February 12, 2008 Author Share Posted February 12, 2008 No, it doesn't change anything. Actually, doesn't it necessary for only string inputs, for numbers don't we just write it without ". Quote Link to comment Share on other sites More sharing options...
Cognito Posted February 12, 2008 Share Posted February 12, 2008 I should of just tested your code first Just put that code on my dev system here and it works perfectly fine, must be something to do with the drupal system some how overriding your value...... Quote Link to comment Share on other sites More sharing options...
aileia Posted February 12, 2008 Author Share Posted February 12, 2008 thanks, cognito. Do you know drupal? Quote Link to comment Share on other sites More sharing options...
Cognito Posted February 12, 2008 Share Posted February 12, 2008 Not enough to pin point whats causing it mate sorry Can I suggest you add the code to a test.php file on your server and make sure that works ? as long as that works you can be 100% sure its drupal having some effect. If not it could be something to do with your php.ini setup. Quote Link to comment Share on other sites More sharing options...
trq Posted February 12, 2008 Share Posted February 12, 2008 globals are bad at the best of times, pass this variable into your function. eg; <?php $life=42; function meaningOfLife($v) { print "The meaning of life is $v"; } meaningOfLife(); ?> I would assume drupal uses eval or something to evaluate this code, and obviously globals won't work for whatever reason. As for this.... My bad noticed another mistake - replace this $life=42; with this $life = '42'; Integers are not to be quoted. Quote Link to comment Share on other sites More sharing options...
aileia Posted February 13, 2008 Author Share Posted February 13, 2008 Thanks cognito, I found the solution in drupal's forum, which states globals should be also defined in the main part of the php code. Then it worked fine. Thank you for your help. Quote Link to comment Share on other sites More sharing options...
deadimp Posted February 17, 2008 Share Posted February 17, 2008 To elaborate, the eval() call that thorpe was talking about is most likely made inside of a function scope, meaning that $life would be a local variable to that function and wouldn't be a global, as you found out. Quote Link to comment 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.