7 easy ways to get your questions answered

Arghhhh – Just Help Me!

Have you ever got so frustrated when no-one answered the PHP question you posted that you just felt like giving up?

For some of us, finding the time to learn PHP (or any other language for that matter) is a downright struggle. We’ve been tasked with getting our project or assignment complete and quite frankly, we don’t have the time to learn everything we need to know about coding – that’s what forums are for right?

Well, just like you I’ve had posts without any answers – and it hurts! Many of my questions have been in desperation as I’ve been battering my head on my desk after spending several hours failing to find a solution. As if I wasn’t mad enough as it is, after posting my forum question nobody answers! And then…

Was that comment really necessary?

Worse still, some so-called expert chips in a very unhelpful comment about me not including code tags in my post. Eh, what? Its usually followed-up with “we are very happy to answer your questions if you help us by posting it correctly”… help, how do I do that?

But wait, there is love in the air…

Here’s the thing though, all the experts that answer questions on the forums do it because they love it. They absolutely love helping other people. They get their kicks from sharing what they have spent hours and hours and hours learning, experimenting with and testing, testing, testing.

Just look at how many questions they all answer, every day, every week! So many of the experts have been on the forums for years, and it doesn’t matter whether this is your very first question, whether you are a beginner, just dipping into PHP to complete that project, or a seasoned coder – they actually want to help you! That’s right, they actually do!

Erm, OK, then why….

You may now be wondering…

“So why didn’t they answer my post then?”
“English is not my first language, why do they penalise me for that?”
“I’m brand new to PHP and to forums, I just don’t know where to start”

They are banging their heads as well!

And do you know what? The experts are just as frustrated as you are! They are itching to help you but they can’t. They are also banging their heads on their desks because they just cannot even start to solve your problem.

On a forum I read regularly, one expert was so mad he said it made his “blood boil!”. Wow, he was one angry expert.

Wahoo, here’s the best bit!

The good news is that it is so easy for all that frustration to just melt away. In fact, there is no need for it to be there at all.

The even better news is that you, yes even you, can get your questions answered by the experts. And, all in good time for you to finish your project. In fact, they will be answering your questions before anybody else’s!

After trawling through hundreds and hundreds of forum posts, and reading and listening to what the experts themselves have to say, I have compiled a simple 7-point check list that you can use right now to get the answers you need. I’m sure the experts can expand on this even more, because like you I’m still learning every day. If you are an expert, I want to help you too so let me have your suggestions!

Here’s the checklist

  • Read the forum rules and guidelines
  • Give the context and be specific
  • Post the error message
  • Describe the behaviour
  • Comment your code
  • Use code tags
  • Include line numbers

Pretty simple isn’t it!

Here is a PDF version for you to download, print off and keep.

If you want a nicer looking A5 version, its here!

Please feel free to share with whoever you wish!

And here’s a bit more info on each point


Read the forum rules and guidelines
Almost every forum has a set of rules and guidelines. Often, these are in a “pinned” post and highlighted…

Forum rules
Pinned post example

Sometimes you need to choose a menu option or tab to see the rules and guidelines…

Forums rules
Menu navigation to rules

However they are shown, if you want your questions answered then you need to be following the rules.

Give the context and be specific
The more specific you are about what it is you are trying to achieve, and exactly what the nature of your issue is, the quicker your question will get answered.

At all costs, avoid phrases like “it doesn’t work”, “it won’t run”, “what’s wrong with my code”. Unless you’ve been specific and given context, these phrases mean nothing and just really makes the experts mad!

Include the error message
If you have an error message from your code, include it exactly as it is. This is a huge help to anyone trying to solve your problem.

Describe the behaviour
What behaviour do you want the code to be doing? Let them know!
What behaviour is it actually displaying? Let them know!

Comment your code
Even a couple of well chosen words to comment a line or block of code can really help with the debugging process. If the experts know what you are trying to achieve and what you think the code should be doing, they can help you faster.

Use code tags
When including your code in your forum post, it is important that everyone can read it correctly. After you have posted your question, many forums also allow the code to be shown in a separate popup or even to be printed. This is a huge help to anyone trying to solve your issue. But it probably won’t work properly if you don’t use code tags. What are code tags?

Here is an example for posting on the Dream In Code forum:

Dream In Code also accepts [php] [/php] tags around your code.

Similarly,  the PHP Freaks forum also uses [code] [/code]  and [php] [/php] tags.

The individual forum guidelines will always let you know what tags are accepted.

Include line numbers
If your IDE/coding tool displays line numbers, include these with your code fragment. Again, this is a huge help to responders, especially if the error message points to a specific line.

Here’s the link to the PDF version of the checklist again

Here is the colourful A5 version

And finally…

Although the experts love to help you, they also want to see you helping yourself. The more they see that you are thinking for yourself as the problem unravels and solutions appear, the more they are likely to continue helping. Here are some of the things they say:

“Don’t be afraid to experiment!”

“Make mistakes”

“Become a contributing part of the team”

“Take our suggestions and yes, try them, but then come up with your own”

If you are afraid that you will lose your work or break something if you experiment, then copy and paste it into a text editor before you make changes, or use source code control for this.

Here’s the link to the PDF version of the checklist again

Here is the colourful A5 version

Huge Free Bonus!

One huge added bonus that you get completely free by following the check list, is that it will start you on your way to effective debugging. Just think about how much time you will save when you can spot the errors yourself! Not only that, but your learning will be accelerated and your project will be completed that much quicker and painlessly!

 

Some Other Resources

I’ve received some really good additional resources about this subject and think its a great idea to share them:

http://blog.jondh.me.uk/2012/09/how-to-use-stack-overflow/ http://slash7.com/2006/12/22/vampires/ http://whathaveyoutried.com/


To get more of your questions answered, sign up to my newsletter now! Make sure you get all the latest PHP tips and tricks delivered directly to your inbox:

Removing the bad smell of global variables

There is always a lot of discussion about good coding practice in PHP, and considerable disagreement when it comes to some areas.

It can be really quite confusing, especially when learning PHP, and many of us just take the easy option because we can’t find agreement on an alternative.

Fortunately, there are some areas where there is general consensus on the best way forward. Global variables is one such area.

Why shouldn’t I use global variables?

While using global variables can seem very easy and convenient, they are often viewed as bad coding practice in PHP for several reasons:

  • They make debugging harder
  • You can get  unexpected results if they are modified without other parts of the code knowing
  • It can make code less portable
  • Security can be compromised

What’s that smell?

So, global variables can lead to a bad code smell and we all know that everyone wants to avoid unmanageable and badly designed code.

But what’s the alternative?

Keeping it simple

One of the easiest ways is to pass the values to your function using parameters. So instead of code that uses global variables like this:

 

We can pass values to the function like this…

 

Here, the variables are local only to the function and this makes debugging in particular, much much easier.


Whether you found this post removed a bad smell for you, or made you smile knowingly, sign up to my newsletter to make sure you get all the latest tips in your inbox:

 

Super-quick tip to save time debugging

We all know that debugging PHP can be (ahem) a challenge… You can make your life a little easier by putting a couple of lines at the top of your script:

<?php
error_reporting(E_ALL);
ini_set(“display_errors”, 1);

Wow, that was easy!

Get lots more easy PHP tips and tricks directly to your inbox by signing up to my newsletter:

One easy way to select earlier dates in MySQL

I recently read a post asking how to select entries in MySQL based on a date field. The original poster only wanting to choose those records that were newer than 7 days old.

This seems quite a common challenge that new PHP coders have, so here is an easy way to achieve it.

  • Firstly, check the date field is indeed a date (or datetime) field and not a varchar. Date comparisons cannot be done on varchar fields
  • Use the MySQL function: DATE_SUB(date, INTERVAL expr units)

One example is:

SELECT myField FROM myTable WHERE myDateField >= DATE_SUB(CURDATE(), INTERVAL 7 DAY);

However if the date field you want to use is actually a DateTime field, the above code may not work as the time throws things. In this case, you need to just be using the date part. For this we can use the MySQL DATE() command. So, the query now looks like this:

SELECT myField FROM myTable WHERE DATE(myDateField) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY);

Here is the link for the official docs on DATE_SUB() and DATE_ADD();

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

 


 

If you found this post helpful, sign up to my newsletter and get PHP tips sent directly to your inbox:

 

How secure is storing your MySQL connection data in a separate file?

Storing your MySQL database connection information in a separate file is secure and safe as long as you follow best practice:

  • The file must have a .php extension
  • All the connection information should be inside your PHP code, and none of it should be output to the browser.

If the file has the .php extension, it will be run as server code and is not visible to any would-be snoopers as there is no client-side output.

You can test this yourself by pointing your browser to your connection file (e.g. www.yourdomain.com/yourconnectionfile.php).

Make sure there is no browser output and check the page’s source code. If it is just a blank page then all is good.

If you see any of your connection data, you will need to check the above two points have been implemented correctly.

A further recommendation would be to locate the connection file in a folder that cannot be accessed publicly. Often, this can be a folder on your web server higher up in the tree that is accessible to the scripts but not the public.


If you want PHP tips and tricks delivered directly to you, make sure you sign up to my newsletter: