PHP noob can't execute

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joeyipextra
Forum Newbie
Posts: 6
Joined: Mon Jan 17, 2011 1:11 am

PHP noob can't execute

Post by joeyipextra »

Hi, I'm a first time user of php and the following error/problem is a childish one:

I had first discovered this after setting up a block of switch codes - the code enters the first case, and executes the rest of the cases anyway. At first I thought my code setup was wrong, so I wanted to isolate this and found:

Code: <?php break; echo '<b>Hello World</p>'; ?>
Output: Hello World'; ?>

It seems to me that the php code is beginning its execution wrong. From that, I can tell the engine is just not entering the break; statements in my switch block. And someone please enlighten me?

Thanks in advance!
Joe
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: PHP noob can't execute

Post by cpetercarter »

I think we need to see the code of your 'switch' section.
joeyipextra
Forum Newbie
Posts: 6
Joined: Mon Jan 17, 2011 1:11 am

Re: PHP noob can't execute

Post by joeyipextra »

Sure, but it behaves exactly the same as the line of code I posted, but here it is.

Code: Select all

<?
$page = "#page1";
echo $page;
switch($page) {
	case "#page1" : $page = '<b>Introduction</b>'; break;

	case '#page2' : $page = '<b>Portfolio</b>'; break;
}

echo $page;
?>
Outputs: Introduction'; break; case '#page2' : $page = 'Portfolio'; break; } echo $page; ?>
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: PHP noob can't execute

Post by cpetercarter »

You have given your script a name like 'test.html'. It should be 'test.php'.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: PHP noob can't execute

Post by Darhazer »

you are using short open tags (<? instead of <?php )
You have to enable them in php.ini
joeyipextra
Forum Newbie
Posts: 6
Joined: Mon Jan 17, 2011 1:11 am

Re: PHP noob can't execute

Post by joeyipextra »

The main script is labeled *.php instead of *html - actually I have tried both and produces the same result.

I tried both <? and <?php tags, which still yield the same result.. is that because I have not configured my computer/browser properly for php? (which I doubt, my new pc laptop and imac both have adobe cs5 master collection and the online tutorial sights work... i seem to get the problem when i downlaod and run the files from my computer, both computers)
joeyipextra
Forum Newbie
Posts: 6
Joined: Mon Jan 17, 2011 1:11 am

Re: PHP noob can't execute

Post by joeyipextra »

or is php.ini another script i need to add to my web folder and to call/refer to from my main script?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: PHP noob can't execute

Post by cpetercarter »

Sorry, my earlier suggestion was wrong - or rather, it is only half right. The problem is that your script is not running as php. It is going straight to the browser, which tries to interpret it as html and ends up dumping most of it to the screen. Have you actually got php and a web server such as Apache installed on your machine? Google XAMPP for your Windows PC. See http://foundationphp.com/tutorials/php_leopard.php for how to enable php on a Mac.
joeyipextra
Forum Newbie
Posts: 6
Joined: Mon Jan 17, 2011 1:11 am

Re: PHP noob can't execute

Post by joeyipextra »

Ahh. now I see. I wasn't aware that browsers don't automatically detect/process php properly without extra setup. The code works when I uploaded it to the server (instead of running it locally). Well, guess I will be using Dreamweaver's dynamic-related files feature a lot more now!

Thank you everyone for your input!
Post Reply