PHP code is dislaying as literal string at runtime

Please see the attached PHP code snipet.

Scroll down to the bottom to the section labelled //THIS IS WHERE THE TROUBLE BEGINS

I am attempting to build an xml string.
Look at this line...
 $xml  = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n";

When I run this, everything after the last single quote (ie the ."\n"; and all the code after that) displays literally displays on screen in my browser.

When I remove this line it still happens but starts from the next line.

Anyone happen to know why this would be happening?

<?php
 	
 	  // Database connection settings
 	  define("PG_DB"  , "test");
 	  define("PG_HOST", "localhost");
 	  define("PG_USER", "postgres");
 	  define("PG_PORT", "5432");
 	  define("TABLE",   "ways");
 	  
 
 	
 	  $counter = $pathlength = 0;
 	
 	  // Retrieve start point
 	 $start = split(' ',$_REQUEST['startpoint']);
 	
 	  $startPoint = array($start[0], $start[1]);
 	 
 	 
 	 
 	 
 	 
 	  // Retrieve end point
 	  $end = split(' ',$_REQUEST['finalpoint']);
 	
 	 $endPoint = array($end[0], $end[1]);
 	 
 	 
 	
 	  // Find the nearest edge
 	  $startEdge = findNearestEdge($startPoint);
 	 
 	
 	  $endEdge   = findNearestEdge($endPoint);
 	
 	  // FUNCTION findNearestEdge
 	  function findNearestEdge($lonlat) {
 	    
 	    // Connect to database
 	    $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	    
 	    $sql = "SELECT gid, source, target, the_geom,
 	             distance(the_geom, GeometryFromText(
 	                  'POINT(".$lonlat[0]." ".$lonlat[1].")', 900913)) AS dist
 	            FROM ".TABLE." 
 	            WHERE the_geom && setsrid(
 	                  'BOX3D(".($lonlat[0]-200)."
 	                         ".($lonlat[1]-200).",
 	                         ".($lonlat[0]+200)."
 	                         ".($lonlat[1]+200).")'::box3d, 900913)
 	            ORDER BY dist LIMIT 1";
 	    
 	 
 	
 	
 	    $query = pg_query($con,$sql); 
 	    
 	    $edge['gid']      = pg_fetch_result($query, 0, 0); 
 	    $edge['source']   = pg_fetch_result($query, 0, 1); 
 	    $edge['target']   = pg_fetch_result($query, 0, 2); 
 	    $edge['the_geom'] = pg_fetch_result($query, 0, 3); 
 	
 	    // Close database connection
 	    pg_close($con);
 	
 	    return $edge;
 	  }
 	
 	  // Select the routing algorithm
 	   switch($_REQUEST['method']) {
 	
 	
 	
 	
 	
 	    case 'SPD' : // Shortest Path Dijkstra
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                   length(rt.the_geom) AS length, ".TABLE.".id
 	                FROM ".TABLE.",
 	                    (SELECT gid, the_geom
 	                        FROM dijkstra_sp_delta(
 	                            '".TABLE."',
 	                            ".$startEdge['source'].",
 	                            ".$endEdge['target'].",
 	                            3000)
 	                     ) as rt
 	                WHERE ".TABLE.".gid=rt.gid;";
 	                
 	
 	                
 	      break;
 	
 	    case 'SPA' : // Shortest Path A*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM astar_sp_delta(
 	                              '".TABLE."',
 	                              ".$startEdge['source'].",
 	                              ".$endEdge['target'].",
 	                              3000)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;"; 
	      break;
 	
 	    case 'SPS' : // Shortest Path Shooting*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM shootingstar_sp(
 	                              '".TABLE."',
 	                              ".$startEdge['gid'].",
 	                              ".$endEdge['gid'].",
 	                              3000, 'length', false, false)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;";
 	      break; 
 	
 	  } // close switch
 	
 	
 	  //  echo $sql;
 	  // Database connection and query
 	  $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	
 	  $query = pg_query($dbcon,$sql);
 	 
 	  // Return route as XML
	//THIS IS WHERE THE TROUBLE BEGINS...
 	  $xml  = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n";
 	  $xml .= "<route>\n";
 	 
 	  // Add edges to XML file
 	  while($edge=pg_fetch_assoc($query)) { 
 	
 	    $pathlength += $edge['length'];
 	
 	    $xml .= "\t<edge id='".++$counter."'>\n";
 	    $xml .= "\t\t<id>".$edge['id']."</id>\n";
 	    $xml .= "\t\t<wkt>".$edge['wkt']."</wkt>\n";
 	    $xml .= "\t\t<length>".round(($pathlength/1000),3)."</length>\n";
 	    $xml .= "\t</edge>\n";
 	  }
 	 
 	  $xml .= "</route>\n";
 	        
 	  // Close database connection
 	  pg_close($dbcon);
 	  // Return routing result
 	  header('Content-type: text/xml',true);
 	  echo $xml;
 	 
	?>

Open in new window

LVL 6
billy21Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Try putting this at  the very top of your file:
ini_set('short_open_tag','Off');

OR
instead of:

$xml  = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n";

try:
          $xml  = '<' . '?xml version="1.0" encoding="UTF-8" standalone="yes" ?'.'>'."\n";

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
billy21Author Commented:
Hi hielo.  Thanks but that didn't change anything.  I've tried it with short_open_tag on, with it off and also changed the xml string as you suggested both with short_open_tag on and off and it make no difference in any case.
hieloCommented:
>>When I remove this line it still happens but starts from the next line.
are you saying that the "literal" output is the PHP code?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

billy21Author Commented:

Just as a test I tried removing the ? characters from the xml header tag.  I also removed the xml building lines underneath it just for clarity.

When I run the page this displays in my browser...

'."\n"; // Close database connection pg_close($dbcon); // Return routing result header('Content-type: text/xml',true); echo $xml; ?>

Note: The only ? characters in the system now are for the opening and closing php tag.  So what is displaying at the end is the closing php tag.

billy21Author Commented:
"are you saying that the "literal" output is the PHP code?"

Yes and even commented out sections are displayed.  This is the full text that I get when I run the original code posted...

'."\n"; $xml .= "\n"; // Add edges to XML file while($edge=pg_fetch_assoc($query)) { $pathlength += $edge['length']; $xml .= "\t\n"; $xml .= "\t\t".$edge['id']."\n"; $xml .= "\t\t".$edge['wkt']."\n"; $xml .= "\t\t".round(($pathlength/1000),3)."\n"; $xml .= "\t\n"; } $xml .= "\n"; // Close database connection pg_close($dbcon); // Return routing result header('Content-type: text/xml',true); echo $xml; ?>
billy21Author Commented:
The attached code snipet is what I'm currently working with.  And the text below is what is displayed in the browser when I run it.  Maybe I've not implemented your solution correctly?

'."\n"; $xml .= "\n"; // Add edges to XML file while($edge=pg_fetch_assoc($query)) { $pathlength += $edge['length']; $xml .= "\t\n"; $xml .= "\t\t".$edge['id']."\n"; $xml .= "\t\t".$edge['wkt']."\n"; $xml .= "\t\t".round(($pathlength/1000),3)."\n"; $xml .= "\t\n"; } $xml .= "\n"; // Close database connection pg_close($dbcon); // Return routing result header('Content-type: text/xml',true); echo $xml; ?>
<?php
 	ini_set('short_open_tag','Off');
		
 	  // Database connection settings
 	  define("PG_DB"  , "routing");
 	  define("PG_HOST", "localhost");
	  define("PG_PASS", "postgres");
 	  define("PG_USER", "postgres");
 	  define("PG_PORT", "5432");
 	  define("TABLE",   "ways");
 	   	
 	  $counter = $pathlength = 0;
 	
 	  // Retrieve start point
 	 $start = split(' ',$_REQUEST['startpoint']);
 	
 	  $startPoint = array($start[0], $start[1]);
 	  	 
 	  // Retrieve end point
 	  $end = split(' ',$_REQUEST['finalpoint']);
 	
 	 $endPoint = array($end[0], $end[1]);
 	 
 	  // Find the nearest edge
 	  $startEdge = findNearestEdge($startPoint);
 	 
 	
 	  $endEdge   = findNearestEdge($endPoint);
 	
 	  // FUNCTION findNearestEdge
 	  function findNearestEdge($lonlat) {
 	    
 	    // Connect to database
 	    $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	    
 	    $sql = "SELECT gid, source, target, the_geom,
 	             distance(the_geom, GeometryFromText(
 	                  'POINT(".$lonlat[0]." ".$lonlat[1].")', 900913)) AS dist
 	            FROM ".TABLE." 
 	            WHERE the_geom && setsrid(
 	                  'BOX3D(".($lonlat[0]-200)."
 	                         ".($lonlat[1]-200).",
 	                         ".($lonlat[0]+200)."
 	                         ".($lonlat[1]+200).")'::box3d, 900913)
 	            ORDER BY dist LIMIT 1";
 	     	
 	    $query = pg_query($con,$sql); 
 	    
 	    $edge['gid']      = pg_fetch_result($query, 0, 0); 
 	    $edge['source']   = pg_fetch_result($query, 0, 1); 
 	    $edge['target']   = pg_fetch_result($query, 0, 2); 
 	    $edge['the_geom'] = pg_fetch_result($query, 0, 3); 
 	
 	    // Close database connection
 	    pg_close($con);
 	
 	    return $edge;
 	  }
 	
 	  // Select the routing algorithm
 	   switch($_REQUEST['method']) {
 	
 	 	
 	    case 'SPD' : // Shortest Path Dijkstra
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                   length(rt.the_geom) AS length, ".TABLE.".id
 	                FROM ".TABLE.",
 	                    (SELECT gid, the_geom
 	                        FROM dijkstra_sp_delta(
 	                            '".TABLE."',
 	                            ".$startEdge['source'].",
 	                            ".$endEdge['target'].",
 	                            3000)
 	                     ) as rt
 	                WHERE ".TABLE.".gid=rt.gid;";
 	                
 	
 	                
 	      break;
 	
 	    case 'SPA' : // Shortest Path A*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM astar_sp_delta(
 	                              '".TABLE."',
 	                              ".$startEdge['source'].",
 	                              ".$endEdge['target'].",
 	                              3000)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;"; 
	      break;
 	
 	    case 'SPS' : // Shortest Path Shooting*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM shootingstar_sp(
 	                              '".TABLE."',
 	                              ".$startEdge['gid'].",
 	                              ".$endEdge['gid'].",
 	                              3000, 'length', false, false)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;";
 	      break; 
 	
 	  } // close switch
 	
 	   //  echo $sql;
 	  // Database connection and query
 	  $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	
 	  $query = pg_query($dbcon,$sql);
 	 
 	  // Return route as XML
	 
 	  $xml  = '<' . '?xml version="1.0" encoding="UTF-8" standalone="yes" ?'.'>'."\n";
 	  $xml .= "<route>\n";
 	 
 	  // Add edges to XML file
 	  while($edge=pg_fetch_assoc($query)) { 
 	
 	    $pathlength += $edge['length'];
 	
 	    $xml .= "\t<edge id='".++$counter."'>\n";
 	    $xml .= "\t\t<id>".$edge['id']."</id>\n";
 	    $xml .= "\t\t<wkt>".$edge['wkt']."</wkt>\n";
 	    $xml .= "\t\t<length>".round(($pathlength/1000),3)."</length>\n";
 	    $xml .= "\t</edge>\n";
 	  }
 	 
 	  $xml .= "</route>\n";
 	        
 	  // Close database connection
 	  pg_close($dbcon);
 	  // Return routing result
 	  header('Content-type: text/xml',true);
 	  echo $xml;
 	 
	?>

Open in new window

hieloCommented:
strange. Try:
<?php
 	ini_set('short_open_tag','Off');
		
 	  // Database connection settings
 	  define("PG_DB"  , "routing");
 	  define("PG_HOST", "localhost");
	  define("PG_PASS", "postgres");
 	  define("PG_USER", "postgres");
 	  define("PG_PORT", "5432");
 	  define("TABLE",   "ways");
 	   	
 	  $counter = $pathlength = 0;
 	
 	  // Retrieve start point
 	 $start = split(' ',$_REQUEST['startpoint']);
 	
 	  $startPoint = array($start[0], $start[1]);
 	  	 
 	  // Retrieve end point
 	  $end = split(' ',$_REQUEST['finalpoint']);
 	
 	 $endPoint = array($end[0], $end[1]);
 	 
 	  // Find the nearest edge
 	  $startEdge = findNearestEdge($startPoint);
 	 
 	
 	  $endEdge   = findNearestEdge($endPoint);
 	
 	  // FUNCTION findNearestEdge
 	  function findNearestEdge($lonlat) {
 	    
 	    // Connect to database
 	    $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	    
 	    $sql = "SELECT gid, source, target, the_geom,
 	             distance(the_geom, GeometryFromText(
 	                  'POINT(".$lonlat[0]." ".$lonlat[1].")', 900913)) AS dist
 	            FROM ".TABLE." 
 	            WHERE the_geom && setsrid(
 	                  'BOX3D(".($lonlat[0]-200)."
 	                         ".($lonlat[1]-200).",
 	                         ".($lonlat[0]+200)."
 	                         ".($lonlat[1]+200).")'::box3d, 900913)
 	            ORDER BY dist LIMIT 1";
 	     	
 	    $query = pg_query($con,$sql); 
 	    
 	    $edge['gid']      = pg_fetch_result($query, 0, 0); 
 	    $edge['source']   = pg_fetch_result($query, 0, 1); 
 	    $edge['target']   = pg_fetch_result($query, 0, 2); 
 	    $edge['the_geom'] = pg_fetch_result($query, 0, 3); 
 	
 	    // Close database connection
 	    pg_close($con);
 	
 	    return $edge;
 	  }
 	
 	  // Select the routing algorithm
 	   switch($_REQUEST['method']) {
 	
 	 	
 	    case 'SPD' : // Shortest Path Dijkstra
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                   length(rt.the_geom) AS length, ".TABLE.".id
 	                FROM ".TABLE.",
 	                    (SELECT gid, the_geom
 	                        FROM dijkstra_sp_delta(
 	                            '".TABLE."',
 	                            ".$startEdge['source'].",
 	                            ".$endEdge['target'].",
 	                            3000)
 	                     ) as rt
 	                WHERE ".TABLE.".gid=rt.gid;";
 	                
 	
 	                
 	      break;
 	
 	    case 'SPA' : // Shortest Path A*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM astar_sp_delta(
 	                              '".TABLE."',
 	                              ".$startEdge['source'].",
 	                              ".$endEdge['target'].",
 	                              3000)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;"; 
	      break;
 	
 	    case 'SPS' : // Shortest Path Shooting*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM shootingstar_sp(
 	                              '".TABLE."',
 	                              ".$startEdge['gid'].",
 	                              ".$endEdge['gid'].",
 	                              3000, 'length', false, false)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;";
 	      break; 
 	
 	  } // close switch
 	
 	   //  echo $sql;
 	  // Database connection and query
 	  $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	
 	  $query = pg_query($dbcon,$sql);
 	 
 	  // Return route as XML

 	  $xml = "<route>\n";
 	 
 	  // Add edges to XML file
 	  while($edge=pg_fetch_assoc($query)) { 
 	
 	    $pathlength += $edge['length'];
 	
 	    $xml .= "\t<edge id='".++$counter."'>\n";
 	    $xml .= "\t\t<id>".$edge['id']."</id>\n";
 	    $xml .= "\t\t<wkt>".$edge['wkt']."</wkt>\n";
 	    $xml .= "\t\t<length>".round(($pathlength/1000),3)."</length>\n";
 	    $xml .= "\t</edge>\n";
 	  }
 	 
 	  $xml .= "</route>\n";
 	        
 	  // Close database connection
 	  pg_close($dbcon);
 	  // Return routing result
 	  header('Content-type: text/xml',true);
 	  echo  '<' . '?xml version="1.0" encoding="UTF-8" standalone="yes" ?'.'>'."\n".$xml;
 	 exit;
	?>

Open in new window

billy21Author Commented:
That results in this...

\n"; // Add edges to XML file while($edge=pg_fetch_assoc($query)) { $pathlength += $edge['length']; $xml .= "\t\n"; $xml .= "\t\t".$edge['id']."\n"; $xml .= "\t\t".$edge['wkt']."\n"; $xml .= "\t\t".round(($pathlength/1000),3)."\n"; $xml .= "\t\n"; } $xml .= "\n"; // Close database connection pg_close($dbcon); // Return routing result header('Content-type: text/xml',true); echo '<' . '?xml version="1.0" encoding="UTF-8" standalone="yes" ?'.'>'."\n".$xml; exit; ?>
hieloCommented:
on the following lines:
         $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
       
         $query = pg_query($dbcon,$sql);

how do you know the queries succeeded?

Try:
$dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER) or die("Error connection to db");
       
$query = pg_query($dbcon,$sql) or die("Error executing query");
billy21Author Commented:
Hi Hielo,

The problem turned out to be an issue with my web server setup.  It wasn't recognising the php code as anything other than text and thus was displaying it to screen.

Had I not had this issue it still would not have worked due to the issues you have highlighted.  Thanks for your help on this and sorry for the delay it just took some time for me to figure out the web server issues.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.