.php files in MAMP

I am running MAMP on my mac and my files labeled .php are only giving me a blank page when I have HTML and PHP in the same file. The pages are displaying information correctly when there is only HTML or PHP it it alone but not together. Is there something in config that I need to change so that my .php files can be run with both HTML and PHP?

The MAMAP server is up and running correctly and phpMyadmin is running.

Thanks
LVL 2
Courtney WhitingConversion Lead / Information ArchitectAsked:
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.

Dave BaldwinFixer of ProblemsCommented:
No, you have to format them properly.  Please post a small file that isn't working and we'll show you what is needed.
Julian HansenCommented:
Are you putting your php inside script tags? Does your file name end with a .php

Here is a basic example of a .php file that includes both .html and php - please check if this works.
test.php
<!doctype html>
<html>
<head>
<title>Title goes here</title>
</head>
<body>
<p>Html content followed by ...</p>
<?php
echo "<p>Script content</p>";
?>
<p>Followed by more html content followed again by some scripted content</p>
<ul>
<?php
for($i=1;$i <= 5;$i++) {
  echo "<li> List item #{$i}</li>";
}
?>
</ul>
</body>
</html>

Open in new window

Ray PaseurCommented:
In your setup, MAMP is the server, and your browser is the client.  The PHP script runs on the server, creates the HTML, CSS and JavaScript, and packages these into a document.  It sends the created document to the client, which is responsible for displaying the document.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

It's possible that you've got a PHP parse error, but your error_reporting() display_error settings are suppressed.  You can find the settings if you run this script, shown here in its entirety.  You can change these settings so that the errors show up (and then you can fix them)!!
<?php phpinfo();

Open in new window

If you're new to PHP and want some good learning resources, this article can help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Some references from the PHP manual that may help you get the error messages to be visible:
http://php.net/manual/en/function.error-reporting.php
http://php.net/manual/en/function.error-log.php
http://php.net/manual/en/function.trigger-error.php
http://php.net/manual/en/function.ini-set.php (especially Example #1)
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Courtney WhitingConversion Lead / Information ArchitectAuthor Commented:
Here's the code I'm using:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8"/>
	<title>PHP Testing File</title>
</head>
<body>

	<?php
		/**
		*Add a form that will allow you to insert a new record into your colors table.
		*Use the code below as an example:
		*@author Grant Whiting
		**/

		$db = new mysqli('localhost', 'root', 'root', 'Heartland');

		$query = "SELECT * FROM `volunteers`";
		$result = $db->query($query);

		if ($db->connect_errno) {
   			printf("Connect failed: %s\n", $db->connect_error);
    		exit();
		}
	?>
	
	<table border = "1">
		<tr>
			<th>ID</th>
			<th>Name</th>
			<th>Address</th>
			<th>DOB</th>
		</tr>
		<?php
			while ($row = $result->fetch_assoc()) {
				$volunteerID = $row['volunteerID'];
				$name = $row['name'];
				$address = $row['address'];
				$dob = $row['DOB'];
		?>
		<tr>
			<td><?php echo $volunteerID ?></td>
			<td><?php echo $name ?></td>
			<td><?php echo $address ?></td>
			<td><?php echo $dob ?></td>
		</tr>
	<?php
		}
	?>

</table>
</body>
</html>

Open in new window


@Dave Baldwin was helping me out earlier with .php files were not working at all but then through his help I got it working. Then files (like this one) that have both HTML and PHP were not working. I did find that putting this:
AddType application/x-httpd-php .html .htm

Open in new window

and
AddHandler application/x-httpd-php .html .htm

Open in new window

into my httpd.conf and made a new file then it started working. Now that it's working, I am still not sure why. Does anyone maybe know why it's working now?

Also, the name of the file is php_testing.php.

Thanks so much.

@Ray Paseur, thank you for the links. Those are really helpful.
Ray PaseurCommented:
Not sure what's going on in your config, but I use this in my .htaccess file:
AddType application/x-httpd-php5 .html .htm .php

Open in new window

That tells the machine to parse .html, .htm, and .php files through PHP.  That way, I can add PHP statements to the top of an HTML document and they will get executed as PHP.

Sometimes, just restarting the engine is needed to get previously changed settings to "take."

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
Courtney WhitingConversion Lead / Information ArchitectAuthor Commented:
Thank you! Adding that to .htaccess and then restarting MAMP all together is working across all files, now.
Julian HansenCommented:
Does anyone maybe know why it's working now?
I am guessing you named your file with a .html extension - which by default is just served by the web server to the browser and is not processed by PHP.

If you had named it with a .PHP extension it would have been processed through the PHP interpreter - which can process pure HTML files and mixed as well as pure PHP.

As soon as you added the directive to pass .html through the php parser then the page worked.

Bottom line it was the way you named your file that probably was the cause of the original problem.
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.