Category Archives: Chỉ dẫn lập trình

isset vs empty in PHP

It might be confusing as far as I red on many forums that fails to explain the diffrence between isset() and empty().
So let me put it this way :

isset() checks if a variable has a value including ( Flase , 0 , or Empty string) , But not NULL.
Returns TRUE if var exists; FALSE otherwise.

On the other hand the empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.

Comparison example on both isset() and empty()

<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
echo ‘$var is either 0, empty, or not set at all’;
}

// Evaluates as true because $var is set
if (isset($var)) {
echo ‘$var is set even though it is empty’;
}
?>

Useful url rewriting examples using .htaccess

If you are looking for the examples of URL rewriting then this post might be useful for you. In this post, I’ve given five useful examples of URL rewriting using .htacess. If you don’t know something about url rewriting then please check my older post about url rewriting using .htaccess.

Now let’s look at the examples

1)Rewriting product.php?id=12 to product-12.html

It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.

RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

2) Rewriting product.php?id=12 to product/ipod-nano/12.html

SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.

RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2

3) Redirecting non www URL to www URL

If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]

4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz

Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

5) Redirecting the domain to a new subfolder of inside public_html.

Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1

Copy from: http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html

Lập trình shell trên Linux

GREP
grep –v “^[#;]”: xóa các dòng bắt đầu bằng kí tự # hoặc ;

SED
sed “/pattern/d”: xóa các dòng có chứa mẫu
    sed “/^$/d”: xóa tất cả các dòng trống.
sed “4d”: xóa dòng thứ 4
sed “2,5d”: xóa từ dòng thứ 2 đến dòng thứ 5
sed “s/pattern/replacement/[g]”: thay thế mẫu [toàn bộ]
sed –e “1d” –e “s/abc/ABC/”: vừa xóa, vừa thay thế
sed –n 1,3p: hiển thị 3 dòng đầu tiên (head -3)
CUT
echo x=199 | cut –d = -f 2
199
Cắt xâu (kiểu như substring)
echo Toi la ai? | cut –b 1-3
Toi
TR
tr “13” “AC”: thay thế kí tự 1 bằng kí tự A, kí tự 3 bằng kí tự C
tr –d “a”: xóa hết kí tự a (có thể sử dụng \n, \r, \t)
tr –s “ “ “ “: tác dụng tựa trim
AWK
WHILE
while (( )); do
done
CASE IN
case $x in
pattern 1)
    # do something
    ;;    # break
    pattern2)
        #
        ;;
    *)# default
        ;;
esac
IF
if [ condition1 –o condition2 ]; then
elif []; then

else
fi

Shell script đổi đuôi file hàng loạt

Đổi tất cả các file có đuôi là .mp3 trong thư mục D thành đuôi là .wma. Thứ tự tham số truyền vào D .mp3 .wma
d=”$1/*$2”
for f in d
do
    # Kiểm tra xem có đúng là file không (ko phải thư mục)
    if [ -f $f ]; then
        # Lấy tên không có đuôi
        fn=`base $f $2`
        # Tên với đuôi mới (đường dẫn)
        n=”$1/$fn$3”
        # Đổi tên
        mv $f $n
    fi
done

Tạo thư mục trong Java

Giả sử bạn đang ở C:\Java\Tutorial, cần tạo thư mục mới New Folder
boolean success = (new File(“New Folder”)).mkdir();

Giờ nếu ở ổ D chưa có thư mục Java\Tutorial, mà bạn cần tạo thư mục mới New Folder với đường dẫn tuyệt đối (đầy đủ) là D:\Java\Tutorial\New Folder\
boolean success = (new File(“New Folder”)).mkdirs();

Khi sử dụng mkdirs, trong đường dẫn đầy đủ, nếu thư mục nào chưa tồn tại nó sẽ tự động được tạo. Thật là khoái!