unexpected T_STRING

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
ceh2624
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Thu Apr 17, 2014 4:13 pm
Location: North Carolina, USA
Contact:

unexpected T_STRING

Post by ceh2624 » Mon Jul 28, 2014 10:02 pm

This is either a fascinating question to PHP programmers or a silly question with the answer staring me in the face. I wrote a PHP program to do some math to estimate PI based on an equation developed by the late Srinivasa Ramanujan.

It works perfectly on my non-joomla site: http://ncfoothills.net/scripts/estPi.php

But not on my Joomla site: http://www.ncfoothills.net/design/index.php/estimate-pi

The code is posted on both pages in an image. I'll post the math part below:

Code: Select all

function factorial($number) 
{
   if ($number == 0) return 1;
   return $number * factorial($number - 1);
}
/********/
$factor = 2 * sqrt(2) / 9801;
$k = 0;
$term = 0;
$total = 0;
$count = 0;
do
{
	$numor = factorial(4 * $k) * (1103 + 26390 * $k);
	$denom = pow(factorial($k), 4) * pow(396, (4 * $k));
	$term = $factor * $numor / $denom;
	$total += $term;
	$count++;
	$k++;
}while($term > 1e-25);



I'm thinking it is a Joomla thing - any ideas?
Last edited by imanickam on Tue Jul 29, 2014 1:58 am, edited 1 time in total.
Reason: Moved the topic from the forum Performance - Joomla! 3.x to the forum Joomla! 3.x Coding

itoctopus
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4025
Joined: Mon Nov 25, 2013 4:35 pm
Location: Montreal, Canada
Contact:

Re: unexpected T_STRING

Post by itoctopus » Tue Jul 29, 2014 4:32 am

Have you tried adding <?php and ?> to the beginning and end of your code?

Also, try removing the comment from your code.

It is important to note that this is a DirectPHP problem, and really has nothing to do with Joomla's own core.
http://www.itoctopus.com - Joomla consulting at its finest
https://twitter.com/itoctopus - Follow us on Twitter

ceh2624
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Thu Apr 17, 2014 4:13 pm
Location: North Carolina, USA
Contact:

Re: unexpected T_STRING

Post by ceh2624 » Tue Jul 29, 2014 12:33 pm

How very odd...
My code was within the <?php ... ?> container. I only gave a snippet here.

:eek: Removing the comments solved the problem. Is it comments in general or the hash usage instead of the double slashes?
Thanks for the tip any my apologies to the Joomla project for making such an assumption.

By the way: It seems DirectPHP also has a problem with <br />...
has anyone noticed?

ceh2624
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Thu Apr 17, 2014 4:13 pm
Location: North Carolina, USA
Contact:

Re: unexpected T_STRING

Post by ceh2624 » Tue Jul 29, 2014 4:53 pm

By the way: It seems DirectPHP also has a problem with <br />...
has anyone noticed?
This is interesting...

DirectPHP doesn't recognise "<br />" in print or echo statements when entered directly.
However, when a php file is included in the article using [include file.php] or [require file.php], it works just fine. This also goes for commented out lines using the hashmark [#].

I use the CodeMirror editor; I wonder if that has anything to do with it.

Any Ideas?
Last edited by ceh2624 on Tue Jul 29, 2014 6:19 pm, edited 1 time in total.

itoctopus
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4025
Joined: Mon Nov 25, 2013 4:35 pm
Location: Montreal, Canada
Contact:

Re: unexpected T_STRING

Post by itoctopus » Tue Jul 29, 2014 4:58 pm

There's something definitely wrong with the DirectPHP PHP parser.
http://www.itoctopus.com - Joomla consulting at its finest
https://twitter.com/itoctopus - Follow us on Twitter

ceh2624
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Thu Apr 17, 2014 4:13 pm
Location: North Carolina, USA
Contact:

Re: unexpected T_STRING

Post by ceh2624 » Mon Aug 04, 2014 4:01 pm

If anyone is interested, I found another drawback to DirectPHP. global doesn't work either. This code has different results inside joomla.

Code: Select all

<h5>
  Does <i>global</i> work in DirectPHP?
</h5>

<?php  
$a = 6;
$b = 3;
$c = $a + $b;
print "the first result: $a + $b = $c. <br />";
print "Now multiply the terms...<br />";

times();

function times()
{
  global $a, $b;
  $d = $a * $b;
  
  if($d == 18)
  {
    print " YES the result is $d, therefor global works.";
  }else{
    print " NO the result is $d, therefor global doesn't work here.";
  }
}
?>
To get the global variables into the function's scope using DirectPHP, they must be passed in.
At least now I know when something doesn't work and no errors are recorded, it might be the parser's fault.

qrusnell
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sun Aug 10, 2014 12:22 pm

Re: unexpected T_STRING

Post by qrusnell » Fri Nov 07, 2014 6:14 am

ceh2624 wrote:If anyone is interested, I found another drawback to DirectPHP. global doesn't work either. This code has different results inside joomla.

Code: Select all

<h5>
  Does <i>global</i> work in DirectPHP?
</h5>

<?php  
$a = 6;
$b = 3;
$c = $a + $b;
print "the first result: $a + $b = $c. <br />";
print "Now multiply the terms...<br />";

times();

function times()
{
  global $a, $b;
  $d = $a * $b;
  
  if($d == 18)
  {
    print " YES the result is $d, therefor global works.";
  }else{
    print " NO the result is $d, therefor global doesn't work here.";
  }
}
?>
To get the global variables into the function's scope using DirectPHP, they must be passed in.
At least now I know when something doesn't work and no errors are recorded, it might be the parser's fault.
I ran into this same issue with globals. Try declaring the variables as global outside the function as well, so like before they are assigned.

Code: Select all

<?php
global $a,$b;
$a=6;
$b=3;
. . .
Worked for me. Seems to be a DirectPHP thing used by Joomla, perhaps emulating php globals for added security reasons, but I haven't found a clear explanation yet. Seems to be wise just to avoid globals if possible.


Locked

Return to “Joomla! 3.x Coding”