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
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;
}
}
?>
There is a missing ; in title.php fiel which may be causing the problems:
<?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
// ? >
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);
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.
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\ itle.php on line 4. I added the semicolon after the include and no error displays, but the problem from the opening post still persists.
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.
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)?
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.
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?
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!
Notepad is a simple editor that saves PHP files with a correct BOM
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.
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?
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: