Heredoc and Nowdoc in PHP

Heredoc is to double-quoted string. So the inside can be parsed if there is variable. There is bug associated with Heredoc, namely the line with the closing identifier must contain no other characters, except possibly a semicolon (;). Example:

$err = ‘Parse error, unexpected $end in this.php on line xx’;
<<<HEREDOC
This is to show error message “$err” if you put whitespace either at front or end of closing line.
HEREDOC

Nowdocs are to single-quoted strings however. So a nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. This is very useful to parse a large block of embedded dynamic codes. Example:

<<<’NOWDOC’
Within this block, though $err is a variable, the final result would be exactly same as what you are seeing now. The variable “$err” won’t be parsed.
NOWDOC

No escape needed for both methods.

In addition, although you can use any word for heredoc and nowdoc purpose, it is recommended using exactly these words to emphasis the difference as well as the help you to remember what these techniques are called.

http://ca2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

What Happened If Your PHP Not Showing?

If PHP was not enabled, your .php file will be treated as .html file. So, <?php…> will be treated as html tag, which would not show at all. More tricky is, if your codes include ->, the first > will be treated as the end tag for <?php…>. This would confuse you because you would think the first -> has been mistakenly regarded as ?>.

Here are solutions. First, save as following piece of codes as phpinfo.php:

<?php
phpinfo();
?>

Then call it in your browser. It will list all of the configuration information about php. If it shows a blank page, it means your php has not been enabled.

If you are in personal server, run following scripts:

LoadModule php5_module "/path/to/php5apache2_2.dll" #use php5apache2.dll for apache 2.x, use php5apache.dll for apache1.x

AddType application/x-httpd-php .php

If you are in shared server, contact administrator.

The another possibility is your server does not recognize your php codes if they were composited by text editor such as Notepad, which indeed still have some uncleared characters. To overcome this, find a pure cleaning php code editor, such as Crimson Editor, copy to it and save as new .php file.

Quick Sorting Method in SQL Query for Disorderly Sorting Criterion

ORDER BY can be used for orderly sorting criterion only. For disorderly sorting criterion, such as Car, Ship, Airplane, ORDER BY could not accomplish the task. Here is a quick method to use ORDER BY as usually.

Step One: Add order number in front of sorting field by using concatenation function.

Step Two: Using ORDER BY as usually.
Step Three: Using substring function to cut the first character.

Example in T-SQL:

SELECT SUBSTRING(newSort, 2, 8)
, Other_Fields
FROM
(
SELECT CASE WHEN Sort_Field = ‘Car’ THEN ‘1Car’
WHEN Sort_Field = ‘Ship’ THEN ‘2Ship’
WHEN Sort_Field = ‘Airplane’ THEN ‘3Airplane’ END AS newSort
, Other_Fields
FROM Tab
) AS innerQuery
ORDER BY newSort

This method can be used in T-SQL, PL/SQL, etc. For T-SQL, please pay special notice to this: ORDER BY cannot be used for any inner query, it must be placed at outer most query. The solution to overcome this weakness is to either abandon this method or save inner query result to temporary table.

A Key Point in Microsoft ODBC Setting

In Microsoft SQL Server DSN Configuration window, select “Change the default database to” is very important. Else, even test shows no error, database linkage may still have problem.

PIVOT – Turning Rows into Columns

Here we exam various PIVOT functions/methods in MS Access, Oracle and SQL Server.

MS Access:

TRANSFORM does not work properly because it would generate many rows with a lot of zeros. Let us call it seggragation. However, covring it by a SUM() GROUP BY statement will do.

SELECT with IIF() does work perfect. Example: http://koncordpartners.blogspot.com/2009/08/practical-case-of-transpose-query.html

Oracle:

SELECT PIVOT does not work properly because of seggragation. However, covring it by a SUM() GROUP BY statement will do.

SELECT with CASE does work perfect. Example: http://koncordpartners.blogspot.com/2009/08/practical-case-of-transpose-query.html

OVER PARTITION BY does work fine, but not recommended because lack of flexibility. Example: http://baurdotnet.wordpress.com/2009/08/26/select/

SQL Server:

PIVOT does work fine, but strongly not recommended for two reasons: 1, Aggragate function whtin PIVOT can not be recalculated according to your will. 2, T-SQL does not allow ORDER BY working within the inner query, which does cause a lot of difficulties for layout arrangement.

Percentage Format in T-SQL

There is no function to present the percentage format in T-SQL, something like 17.25%. There are various ways to do this. However, for reporting purpose, the easiest way might be:

CONVERT(VARCHAR, Result_From_Calc)+'%'

Following is an example how to add Total row at bottom of the selected dataset as will as add percentage column for each of rows.

The original dataset is:

SELECT Leg_Id
, Statute_Miles
FROM dw_Legs
GO

The final script is as follows:

WITH Totaling
AS
(
SELECT SUM(Statute_Miles) AS Milage
FROM dw_Legs
)
SELECT Leg
, Milage
, CONVERT(VARCHAR, Milage*100/(SELECT Milage FROM Totaling))+'%' AS Percentage
FROM
(
SELECT CONVERT(VARCHAR, Leg_Id) AS Leg
, Statute_Miles AS Milage
FROM dw_Legs
UNION
SELECT 'Total' AS Leg
, Milage
FROM Totaling
) AS Dataset
GO

Explaination:

1. Total milage will be used more than once, so it has been in put into WITH clause.
2. The reason convert Leg_Id into VARCHAR is because word ‘Total’ is a VARHCAR. Else the UNION won’t work.
3. Make sure there is only one row being selected into WITH clause. That is where people usually create a bug.

Following is the result:

Difference Between www and Without www

Put it simple, domain name without www is the more authentic one. This is because HTTP does not specify your domain name must be started with www. or any other prefix. Indeed, www.yourdomain.com is just one of your subdomains.

Then why in early days every domain name starts with www? Well, it is just a common convention without much rationale, something like why at first place domain name on the Earth not like http:com.yourdomain.level-1-subdomain.level-2-sub/parameter-level-1/... We human being just sometimes make irrational stuff. So, early people name www.yourdomain.com as main domain and subdomain.yourdomain.com as subdomain, while technically they are all subdomains.

By now you would know www2., www3., www4. no longer a mystery. By now, most webmasters would have to make sure www.yourdomain.com being redirect to yourdomain.com.

Since www.yourdomain.com is just a subdomain of your main domain, it is no wonder search engines treat them as separate pages. This bad practice now is a norm to direct the yourdomain.com to www.yourdomain.com as part of getting your site SEO.

However, it is suggested to redirect www.yourdomain.com to yourdomain.com by include following code in your .htaccess file, which is located in public_html:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]


http://www.seocompany.ca/seo/url-redirect.html
http://www.bluehostforum.com/showthread.php?t=1019

Labels