SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)

    Strange Characters Displaying with OOP PHP

    Just trying out some very simple OOP PHP and some funny characters are being displayed on the page for what seems to be no apparent reason!

    I've got two PHP files: A class file which is just PHP and a PHP file which contains HTML and calls the method from the class. Here are the two files I have:

    book.class.php

    PHP Code:
    <?php 
    class Book
    {
        
    // Property AKA Variable Declaration
        
    public $bookTitle 'Dracula';
        public 
    $bookSlug '';
        public 
    $bookAuthor '';
        public 
    $bookPublisher '';
        public 
    $bookGenreSubject '';
        public 
    $bookRating '';
        public 
    $bookISBN '';
        public 
    $bookPrice '';
        
        
    // Constructor
        
    public function __construct()
        {
            
    // $this->bookTitle = $bookTitle;
        
    }
        
        
    // Method AKA Function Declaration
        
    public function setBookTitle($bookTitle)
        {
            if(!empty(
    $bookTitle)) 
            {
                
    $this->bookTitle $bookTitle;
            }
        }
        
        public function 
    getBookTitle()
        {
            return 
    $this->bookTitle;
        }

    ?>
    title.php

    HTML Code:
    <?php include_once("includes/book.class.php") ?>
    <?php $title = new Book(); ?>
    <!DOCTYPE html>
    <html>
    	<head>
    		<title><?php echo $title->getBookTitle(); ?></title>
    	</head>
    
    	<body>
    		<article>
    			<header>
    				<h1><?php echo $title->getBookTitle(); ?></h1>
    			</header>
    		</article>
    	</body>
    </html>
    First of all, the title.php page displays this:

    oop1.png

    What the bloody hell is going on here? I don't have a clue! All that should be on this page is "Dracula" wrapped in the h1 tags.

    And when I go directly to the book.class.php page it always displays this:

    oop2.png

    Should the class be displaying anything in the browser like that?

    Any ideas? Help would be appreciated, thanks.

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,149
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Since the PHP source code is showing up in the browser, I suspect that your server isn't setup to handle PHP files.
    "First make it work. Then make it better."

  3. #3
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    There is a missing ; in title.php fiel which may be causing the problems:

    PHP Code:
    <?php
       
    // add the following two lines and the missing semicolon error should appear
       
    error_reporting(-1); 
       
    ini_set('display_errors'true);
       
       include_once(
    "includes/book.class.php")  // MISSING semicolon
      
    $title = new Book();


    // next line is not required and sometimes causes problems
    // ? >

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    It looks like some other script is evaluating those scripts, a stab-in-the-dark do you have some mod_rewrite that might affect the "huffieroop" path?

    Also, what does the actual "HTML" source of those pages look like?
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    There is a missing ; in title.php fiel which may be causing the problems:
    That won't cause problems, the closing PHP tag ends the statement. See the first paragraph of Instruction separation in the manual.
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Salathe View Post
    That won't cause problems, the closing PHP tag ends the statement. See the first paragraph of Instruction separation in the manual.
    I guessed the missing semicolon was causing the problem and did not realise the semicolon was optional if it is the last statement before the closing ?> Thank you.


    Whoops, the reference to the optional ?> was meant for the book.class.php file.

    I usually rely upon the error_reporting(-1); to scream with all the errors.

    // off topic
    Reminds me of one programmer in the last team where I worked who made a special point of setting error_reporting(0);

  7. #7
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    Since the PHP source code is showing up in the browser, I suspect that your server isn't setup to handle PHP files.
    Hey Jeff, I've just installed the latest version of XAMPP for Windows on my new Windows 8 machine and I played around with some other basic PHP statements and they work perfectly fine, so my localhost is setup to handle PHP files.

    Quote Originally Posted by John_Betong View Post
    There is a missing ; in title.php fiel which may be causing the problems:

    PHP Code:
    <?php
       
    // add the following two lines and the missing semicolon error should appear
       
    error_reporting(-1); 
       
    ini_set('display_errors'true);
       
       include_once(
    "includes/book.class.php")  // MISSING semicolon
      
    $title = new Book();


    // next line is not required and sometimes causes problems
    // ? >
    Without the semicolon after the include a parse error displays (this is with the two lines included that you suggested, thanks!): Parse error: syntax error, unexpected '$title' (T_VARIABLE) in C:\xampp\htdocs\huffieroop\title.php on line 4. I added the semicolon after the include and no error displays, but the problem from the opening post still persists.

    Quote Originally Posted by Salathe View Post
    It looks like some other script is evaluating those scripts, a stab-in-the-dark do you have some mod_rewrite that might affect the "huffieroop" path?

    Also, what does the actual "HTML" source of those pages look like?
    Not sure about any mod_rewrite stuff that might affect it. It's a standard install of XAMPP for Windows.

    What's interesting is that I had the same files on my Windows 7 PC with a standard install of XAMPP for Windows and it works perfectly fine - As anyone would expect it to work!

    The difference between my Windows 7 and Windows 8 PCs, other than the operating system itself, is the install of XAMPP for Windows. On my Windows 7 PC I have XAMPP 1.7.4 with PHP 5.3.5 whereas on my Windows 8 PC I have XAMPP 1.8.1 with PHP 5.4.7 installed. But surely the fact that it's a newer stable version of PHP wouldn't affect it?

    Very strange indeed. Any ideas how I could fix this guys? Very vexing problem!

    As for the actual "HTML" source of the pages, see the screenshots below Salathe.

    htmlsource1.pnghtmlsource2.png

  8. #8
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    After the HTML script there is no start for the php script.

    PHP Code:
    </HTML> <?php echo $title-getBookTitle();?><p>
    Pecked from the tablet

  9. #9
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    After the HTML script there is no start for the php script.

    PHP Code:
    </HTML> <?php echo $title-getBookTitle();?><p>
    Pecked from the tablet
    Huh? Confused. What do you mean and where? The "HTML" source displayed in the two screenshots in my previous post aren't the actual code I've typed (that's back in the opening post). That funny HTML in the view source has been generated by either PHP or the browser - For some unusual and perplexing reason!

    Surely it's something wrong with my XAMPP setup on my Windows 8 PC? As I said, the code from the opening post works perfectly fine in the old version of XAMPP on my Windows 7 PC. Is it XAMPP or Windows 8? Or is it actually the code (I can't see it being the code anymore, having said what I just typed)?

  10. #10
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    @AndrewCooper ;

    Now back on my desktop

    copied from your screenshot book.class.php
    PHP Code:
    }
    ?>
    <p></p>
    <p><strong>Title</strong> Dracula</p>
    </body>
    </html>echo $title->getBooktitle();></p>
    </body>
    </html> 
    Try the following:
    PHP Code:
    }
    ?>
      <p></p>
      <p><strong>Title</strong> Dracula</p>
      <p>
        <?php echo $title->getBooktitle();?>
      </p>
    </body>
    </html>
    Please supply the full source files of the two screen dumps. The other screendump from title.php appears as though HTML script precedes <!doctype>. Also, <head> and <body> sections are not in the correct order.
    Last edited by John_Betong; May 19, 2013 at 03:41. Reason: added missing ? from PHP clsoing statement also spelling

  11. #11
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    925
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    There must be some other code you are including in your script that we don't know about and probably you don't know about either. Look, your screenshot shows this:

    Title: Dracula

    In neither of your source files is there an occurrence of "Title:", there even is not a single colon there nor any occurrence of styling that would make it bold. So it looks like you got something out of nothing and this doesn't make sense from a programmer's point of view! I don't know what's going on there but I'm sure there's more to it that you posted in your question

    Edit: I don't know why your attachments show as "pending approval" and can't be accessed. Can this be some forum configuration error?

  12. #12
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    It looks like some other script is evaluating those scripts
    Quote Originally Posted by Lemon Juice View Post
    There must be some other code you are including in your script that we don't know about and probably you don't know about either.

    In neither of your source files is there an occurrence of "Title:", there even is not a single colon there nor any occurrence of styling that would make it bold. So it looks like you got something out of nothing and this doesn't make sense from a programmer's point of view! I don't know what's going on there but I'm sure there's more to it that you posted in your question
    You're both right!

    See, it works perfectly on my Windows 7 PC because I created and edited the PHP files using Windows Notepad.

    On my Windows 8 PC I created and edited the PHP files using a Windows 8 App called "Code Writer". It's a bit like a simplified version of Notepad++, but for the Metro UI. Anyway, it seems that this app was adding in bits of code I didn't want adding in upon saving each time.

    Pesky bugger!

    Solution: Uninstall "Code Writer" and revert back to using trusty old Windows Notepad on both my Windows 7 and Windows 8 PCs.

    Problem solved.

    But wasn't that a funny one, eh? Very strange. I've never heard of anything like that happening before, have you guys? And sorry - In hindsight I should have mentioned what editor I was using to create and edit the PHP files as that may have resolved the problem much sooner!

    Thanks for the help guys

  13. #13
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    @AndrewCooper ;

    Notepad is a simple editor that saves PHP files with a correct BOM

    http://www.w3.org/International/ques...er-mark.en.php

    I should imagine that "Code Writer" defaults to an incorrect BOM. I have had the same problem using UltraEdit and solved by copying, pasting and saving the files using Notepad. UltraEdit loads and saves the file with the BOM that PHP expects.

    Currently I am using "Sublime Text 2" editor and have not had the BOM problen. The editor is highly recommended by many programmers.

  14. #14
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AndrewCooper View Post
    You're both right!

    On my Windows 8 PC I created and edited the PHP files using a Windows 8 App called "Code Writer". It's a bit like a simplified version of Notepad++, but for the Metro UI. Anyway, it seems that this app was adding in bits of code I didn't want adding in upon saving each time.

    Pesky bugger!
    Glad you found the problem. Pesky bugger indeed!
    Salathe
    Software Developer and PHP Manual Author.

  15. #15
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    Currently I am using "Sublime Text 2" editor and have not had the BOM problen. The editor is highly recommended by many programmers.
    Thanks for the recommendation John, I checked it out and I love it! The problem I had all these years in switching from Notepad to something with more features like Notepad++ was the UI and just the general feel of it. I loved the clean canvas feeling of Notepad, but Sublime Text 2 is just perfect and will definitely help me code more productively too.

    Is there any real difference between the free and paid versions though?

  16. #16
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by AndrewCooper View Post
    Thanks for the recommendation John, I checked it out and I love it! The problem I had all these years in switching from Notepad to something with more features like Notepad++ was the UI and just the general feel of it. I loved the clean canvas feeling of Notepad, but Sublime Text 2 is just perfect and will definitely help me code more productively too.

    Is there any real difference between the free and paid versions though?
    From their website:
    Sublime Text 2 may be downloaded and evaluated for free, however a license must be purchased for continued use.
    There is currently no enforced time limit for the evaluation.
    After about a month a nag appears which can be accepted then editing continues. It is on my list of Shareware I will buy

    Also on my list of Shareware to Donate is this unobtrusive but remarkably capable FireFox HTML Validator Addon:

    http://users.skynet.be/mgueury/mozil..._upgrade4.html


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •