tags:

views:

58

answers:

2

I am new to PHP and am trying to write a very simple class:

<?php
    class Course {
        private $credits;
        public function getCredits() {
        return $this->credits;
        }
    }   
?>

the problem is when I load this in a browser, I see:

credits; } } ?>

so it looks like the browser is echoing all that follows "this->" - is there something I need to configure in php.ini? thanks for any help!

+8  A: 

You can't just load the .php file directly into your browser through the open dialog.

The reason you get that result is

<?php class Course { private $credits; public function getCredits() { return $this->credits; } }
?>

Is being interpreted as a single (invalid) html tag

<?php class Course { ... $this->

Then the rest of the stuff is assumed to be plain text.

The php stuff needs to be parsed by the php interpreter before loaded to the browser. This is usually done through running a web server with php support - like Apache.

Checkout XAMPP for a quick way to get started with this

XAMPP - http://www.apachefriends.org/en/xampp.html

Jamie Wong
+1 Oooh is that what he meant ....
Phil Wallach
Are you opening the file through the browser or executing by going to `http://localhost/page.php`?
Jamie Wong
I have done both - through the open file dialog of the browser as well as directly typing the URL in the navigation bar
I give up! I finally went to bed last night, woke up today and just tried loading the page again...and the problem is gone. NUTS! Can anyone explain this weirdness?? I did not touch any of the files in my sleep, I promise...
A: 

As it turns out, the problem I was having was due to some kind of bug in my installation of XAMPP. The problem shows up only intermittently, and it does look like the ">" is being interpreted as a closing tag. But I could take the same code and run it on a remote web site with no problem. And 50% of the time, it works fine on my own server too. Still curious if anyone else has seen this problem though?