We help IT Professionals succeed at work.

PHP quote problem

aaharrison
aaharrison asked
on
Hi, the following is a snippet from a large system I am creating.  When I run this, nothing happens - nothing is output to the screen.  What am I doing wrong?  I would like to just print out the contents of $phpFile.

Thanks!

<?php
$primaryField[0] = "got it";
$fn = "more stuff";
$phpFile = "
	<?php 
		if (checkRecord(".$primaryField[0].", ".$fn.", 'chklst') == 'no' && checkRecord($serverID, 'server', 'serverinfo') == 'no')
		{
			//insert into both databases
			
			if (addToCHKLST() == 'success')
			{
				if (addToServerInfo() == 'success')
				{
					echo 'Add into both databases! ';
				}
				else
				{
					echo 'error occurred';
				}
			}
			else
			{
				echo 'error occurred';
			}
			
		}
		";
echo $phpFile;
?>

Open in new window

Comment
Watch Question

Commented:
error_reporting(E_ALL);
ini_set("display_errors", 1);

And check for errors.
Except for the undefined reference to $serverID, it is working fine for me.

The reason why you are not able to see the output is because the file is not in HTML format. For ex, <?php should be displayed as <?php

So use echo htmlspecialchars($phpFile);

    if (addToCHKLST() == 'success')
                        {
                                if (addToServerInfo() == 'success')
                                {
                                        echo 'Add into both databases! ';
                                }
                                else
                                {
                                        echo 'error occurred';
                                }
                        }
                        else
                        {
                                echo 'error occurred';
                        }
                        
                }
                ";
echo htmlspecialchars($phpFile);
?>

Open in new window

I meant to say <?php should be displayed as &amp;lt?php (EE decoded the string I typed there)

Author

Commented:
THANK YOU SO MUCH!  That has been driving me crazy.  I do have one more question though..

say for example where I have $serverID - how can I make it print $serverID and not interpolate it?

Commented:
echo "\$serverID";
Use single quote instead of double quote.. So that the variable won't be expanded

Author

Commented:
i have to be able to have half the string interpolate and the other half not - so when i surround the $serverID in single quotes it still gets interpolated.  
Then you have to use double quotes and escape $ for the variables as shown by wizzie83..
<?php
$primaryField[0] = "got it";
$fn = "more stuff";
$phpFile = "
	<?php 
		if (checkRecord(".$primaryField[0].", ".$fn.", 'chklst') == 'no' && checkRecord(\$serverID, 'server', 'serverinfo') == 'no')
		{
			//insert into both databases
			
			if (addToCHKLST() == 'success')
			{
				if (addToServerInfo() == 'success')
				{
					echo 'Add into both databases! ';
				}
				else
				{
					echo 'error occurred';
				}
			}
			else
			{
				echo 'error occurred';
			}
			
		}
		";
echo htmlspecialchars($phpFile);
?>

Open in new window

Author

Commented:
great thank you so much, you give great explanations!