Page 1 of 1

PHP noob can't execute

Posted: Mon Jan 17, 2011 1:21 am
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

Re: PHP noob can't execute

Posted: Mon Jan 17, 2011 1:26 am
by cpetercarter
I think we need to see the code of your 'switch' section.

Re: PHP noob can't execute

Posted: Mon Jan 17, 2011 2:13 am
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; ?>

Re: PHP noob can't execute

Posted: Mon Jan 17, 2011 9:18 am
by cpetercarter
You have given your script a name like 'test.html'. It should be 'test.php'.

Re: PHP noob can't execute

Posted: Mon Jan 17, 2011 9:21 am
by Darhazer
you are using short open tags (<? instead of <?php )
You have to enable them in php.ini

Re: PHP noob can't execute

Posted: Mon Jan 17, 2011 10:25 am
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)

Re: PHP noob can't execute

Posted: Mon Jan 17, 2011 10:28 am
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?

Re: PHP noob can't execute

Posted: Mon Jan 17, 2011 5:18 pm
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.

Re: PHP noob can't execute

Posted: Tue Jan 18, 2011 2:21 pm
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!