Getting a single character in a string

October 9th, 2007

This snippet of code will show how to get individual character from a string in a variable

$sMyName = “Darwin”;

print $sMyName[1]; // will print the letter “a”

How to use heredoc in PHP

October 9th, 2007

Heredoc also known as here-document is a way of specifying string literals or printing them literally without the use of quotes to threat them as literal strings. Heredoc preserves the line breaks and other whitespace (including indentation) in the text.

Heredocs start with <<< and a token or also known as delimiting identifier. The token or delimiting identifier don’t leading or trailing whitespace and followed by semicolon a to end the statement.

Example: We wanted to print this string

The quick brown fox
jumps over the lazy dog.
But what is the name of that dog 
whom the fox jumped over.

Usually we will use the print built in function of php and put line breaks to separate the lines

print “The quick brown fox \n”;
print “jumps over the lazy dog. \n”;
print “But what is the name of that dog \n”; 
print “whom the fox jumped over \n”;

In heredoc we literally print these sequence string without having to print all the lines 1 by 1;

Using heredoc

print <<< DOGS
The quick brown fox
jumps over the lazy dog.
But what is the name of that dog 
whom the fox jumped over.
DOGS; 

Heredocs in HTML

$sAnimal = “cat”;
$aZoo = array(”elephant”,”zebra”,”monkey”,”lion”,”bear”);
print <<< ZOO
There are different animals in the zoo.
It includes <b>$aZoo[1], $aZoo[3].</b>
There are also <font color=”red”>$sAnimal</font> here.
ZOO;

My PHP Coding Standard

October 7th, 2007

Before I start writing my own PHP tutorials, I would to set the coding standards I will be following in the course of this tutorial. This aims to standardize all my snippets ( small chunk of code) in all examples I will be giving. I would like to emphasize that this is not a standard to all PHP programmers but just my personal preferences which we all do have.

Comments - - comments are piece of information that programmers usually put to their code to explain what it does. Comments are not actually part of the execution of a program but instead they are ignored by the PHP interpreter. It is a good practice in a programmer to put comments on his code because in the future, comments are the only guide a programmer can follow to understand a big pile of code. Comments may vary depending on what type comment to put.

1. single line comments - for single line commenting I use the double forward slash //

Example:

// this line will print your name
print "My name is Juan dela Cruz";

2. multi-line comment - for multi line I use /* and the closing */

Example:

/*
This is a multi-line comment
and goes all the way
up to this point
*/

Variables

for variables I use the camelize form. Meaning letters in my variable have combination of upper and lower case. But in addition I am affixing a single letter prefix that will determine the datatype of that variable. For example, if I would declare an array variable I would write $aMyArray.

Example:

$aMyArray = array(1,2,3,4); // contains array values
$sYourName = "Conai"; // contains string values
$iLoop = 100; // contains integer values
$oDBConnection = mysql_connect("localhost", "username", "password"); // object
$bIsTrue = false;  // boolean
$mMixedVariable = "test"; // mixed variable, a variable that may vary in datatype

For single usage or single letter variable there is an exemption on my rule.

for ($x=1; $x<count($aMyArray); $x++) {
    <statement here>
}

in the above example $x dont have any prefix on what datatype it is. I have sited this as my exemption.



Constants

as a rule constants should all be in uppercase and words separated by underscore(_)

Example:

define('HOST',  'localhost');
define('USER_NAME', 'root');
define('PASSWORD', '1234567a');



Conditional Statements

if statement

if ($x == 1) {
    <statement here after 4 solid spaces>
}

if - else statement

if ($x == null || $y != false) {
    <statement of if after 4 solid spaces>
} else {
    <statement of else after 4 solid spaces>
}

if -else if - else

if ($x > count($aResults)) {
    <statement of if after 4 solid spaces>
} else if ($x < count($aResults)) {
    <statement of else fif after 4 solid spaces>
} else {
    <statement of else after 4 solid spaces>
}

Loop statement

for loop

for($iCounter=0; $iCounter<10; $iCounter++) {
    <statement of for loop after 4 solid spaces>
}

while

while( !feof($oHandle) ) {
    <statement of while loop after 4 solid spaces>
}

do-while

do {
    <statement after 4 solid spaces>
}  while( $iLoop > 10) ;


Functions - function names are the same as the camelize form of my variables. Function names usually starts with small letters.

Example:

function checkThisVariable($mVariable) {
     if( is_integer($mVariable) ) {
        return "integer";
    } else if ( is_array($mVariable) ) {
        return "array";
    } else {
         return "undetermine";
    }
}

Class - class name are also in camelize form but the first letter is Capitalize to signify that it is a class when placed among other variables.

Example:

class Cabinet {
    var $sDrawer;
    function Cabinet () {
        $this->sDrawer = true;
    }
}

				

Keyword “filipino php programmer”

October 6th, 2007

Wow! is my expression when I found out that my site is #1 on the list when you search the keyword “filipino php programmer” in google.com. At first I cannot believe that my site was first on the list, with the fact that I just written only two topics on my blog. I have come to think that relevance in the keyword really means a lot. Well basically I can say that my site has the relavance since I didn’t write so much about different topic. This has inspired me to write more but following that keyword as my overall topic in my blog.

Setup Smart MMS and GPRS

October 5th, 2007

For Smart Telecom subcriber here is a way to setup your GPRS connection using your handheld.

For MMS
1. Create a message and type in the following

SET MMS <phone model>

For example if I have a nokia 6233 I would type in

SET MMS N6233

and send it to 211

For GPRS

SET GPRS <phone model>

For example if I have a nokia 6233 I would type in

SET GPRS N6233

and sent 211

2. You will then receive a couple of message which are settings for the MMS and GPRS. All you need to do is save them in your connection settings.

3. Usually you will have to wait for less than an hour for this settings to work.

4. To test if the MMS is working, try to send some pictures to yourself via MMS.

5. To test your GPRS for internet connection, usually I do this trick for nokia handheld. I press+hold the “0″ (zero) key and wait for the browser to launch.

6. If still your setup is not working. Try calling *888 for customer support

Mind of the author - blogging

September 30th, 2007

It is not in my nature to be expressive. Its always been so hard for me to express my thoughts may it be in words or in writing. But due to the nature of my job being in the IT industry, I cannot help but to be expose to this new trend of putting your inner thoughts and ideas in writing and have it posted on the web termed as Blogging.

Now is my chance to develop one of my weaknesses in life. The start of bursting emotions, ideas, commentaries, essays and other stuff that may or may not be helpful to other readers that might have accidentally drop to my station. This is the breaking ground of the writer part of me. A new venture into the world of words. Spontaneous appearance of vocabulary words I’ve learn from school days. And most of all the discovery of the real me.

This is the mind of the author.