AJAXification by js graceful degradation

Now the question was about a suggestion to have a balance between best designs considering a friendly URL, Ajax, and SEO. It was implied about some contradictory indications. With the discussion passing on to friendly URLs are great if for no reason than log analysis, however, friendly URLs are supposedly better for SEO also (besides all
the other stuff for SEO).

Suppose if you would like to start moving over to AJAX for CMS-related, stuff, such as loading a news article when the user clicks on a headline, and stil make the site really search engine friendly.

Basically, you have a regular tag link that goes to the location you want, but you also have an onclick javascript event for that link that does the ajax stuff and returns false so that the browser doesn’t request the href part of the tag. Ideally, you’ll have an onload event to your page that attaches all these onclick events to your links so that you don’t even have inline javascript. (Rob Marscher at NYPHP).

The added benefit to backloading your ajaxification is that regardless of js enablement, the user could right click your link and open in new tab or window or even bookmark it and it’s still a plain link that will open normally. (Mark Armendariz at NYPHP).

The overall suggestion is to use prototype.js, and some implementation to use asynchronous fetch.

User input in php command line

Ever wondered how to capture the user input when writing php command line scripts ?

<?php
 
function getInput($msg){
  
fwrite(STDOUT"$msg: ");
  
$varin trim(fgets(STDIN));
  return 
$varin;
}
 
?>

The function above is being used by me in certain command line scripts, where I need user responses.

PHP – Quotes or No quotes in associative Array?

Our team was having a debate on why we should put a ‘ (single quote) when referencing arrays in php. Well what is the problem if we use $_SESSION[key], instead of $_SESSION[‘key’].

Well when you think that when encountering non functions or keywords, php tries to evaluate it as a declared constant. So referencing $_SESSION[‘key’] would be better against $_SESSION[key], unless we have defined the constant key and want to use the value of key.

Single Quotes vs Double Quotes!

I recommend using ‘ (single quotes) when programming with PHP; Always use ‘ (single quotes) unless you need the features of ” (double quotes). You might think it’s much easier to write code as:

echo "Today is the $day of $month";

However, using single quotes forces variables to be outside the quotes; instead when you use the ” (double quotes), forces php to evaluate the string, where as with ‘ (single quotes), the string content is taken as such.

error_reporting(E_ALL); make code faster

The single most important thing I tell people who use PHP is to turn error reporting to its maximum level. Why would I want to do this? Generally the error reporting is set at a level that will hide many little things like:

  • declaring a variable ahead of time,
  • referencing a variable that is not available in that segment of code, or
  • using a define that is not set

These factors might not seem like that big a deal — until you develop structured or object oriented programs with functions and classes. Too often, writing code with the error reporting turned up high would cost you hours as you scoured long functions that didn’t work because a variable was misspelled or not accessible.

PHP won’t tell you anything in that case it’ll just create the new variable for you and initialize it to zero. The remedy is to put the following line at the top of every PHP document as you develop:

error_reporting(E_ALL);

It simply forces the error reporting to be at its highest level. Try putting this line in other PHP programs, and more often than not you’ll receive a barrage of warning messages that identify all the potentially wrong elements of the code.

Eventually this when enabled, will force you to write cleaner, structured and fast code.