Php tutorials

August 15th, 2007 by Sajith M.R

php

Before starting php tutorials >>>

Hai friends,

Before you starting the php programming, one thing i
would like to inform you that, you need to know the bare basics of webserver
and client server architecture. What is web server? What is
apache ? What is javascript ? What is ajax ?

Go http://course.sajithmr.com/content.php?cid=2
for getting bare basic knowledge.

After these knowledge you can use

http://www.course.sajithmr.com/downloads/php/php_manual_en.chm

this file for your further php study.

Thanks and Regards
Sajith.M.R

Substring - PHP

August 15th, 2007 by Sajith M.R

sub string php

More about PHP substr() function >>>

You know substr()
function in php. The normal usage of this function is here:

Sytax: substr($str, $start, $length);

<?php echo substr(’hello
world’,7,3); ?>
// First parameter is the string, second is
the start index (first character of the string starts from 0) and third param
is the length of the substring

Output: orl .

If we not specified the length parameter (parameter 3),
returns the string from $start to the end of the string.

example:
<?php
echo substr(’hello world’,7); ?>

gives the output: orld

If the $start parameter is a negative number, the position
starts from end of the string. Example:

<?php
echo substr(’on the way to operating theatre’, -9) ?>

gives an output: g theatre

another example:

<?php
echo substr(’on the way to operating theatre’, -9,3) ?>

output: g t

If $length is negative, substr( ) counts back from the end of the string to
determine
where your substring ends:

eg: <?php
echo substr(’on the way to operating theatre’, 4,-3) ?>
output: he way to operating thea

another eg: <?php
echo substr(’on the way to operating theatre’, -9,-3) ?>

output: g thea

Thanks and Regards

Syth

Heredocs

August 15th, 2007 by Sajith M.R

heredocs.jpg

Heredocs >>

You know php heredocs ?? In php print function if we need to print a data which
has a big length and size, it is very complecated to enclose whole in single
quotes or in double quotes. If there is any single quotes present inside our
data , and we are using single quotes for string representation, php compiler
will show syntax error.

In these cases we can use heredocs. <<< is the symbol
for heredocs. Heredocs starts with this symbol (<<<) and end with a
token. See example:

<?php

print <<< UPTOEND

I can write any thing here. “This is my name”.
This is a new syntax.I
can use ’single quotes’ also.
php is a good programming
language.
my website is sajithmr.com

UPTOEND;

?>

Here UPTOEND is the token. The above code will give the
output:

I can write any thing here. “This is my name”.
This is a new syntax.I
can use ’single quotes’ also.
php is a good programming
language.
my website is sajithmr.com

But the thing to remeber is never confuse with the token
(delimiter)

<?php
print <<< CATS
If you like pets, yell out:
CATS AND DOGS ARE GREAT!
CATS;
?>

This will give the output:

If you like pets, yell out:
CATS AND DOGS ARE GREAT!

PHP Testing

August 15th, 2007 by Sajith M.R

php testing

PHP Testing >>

The main problem i faced during php programming the lack of quick testing.
For Example if i want to test the function substr(), how this is working, or
what is the result of file_get_contents function, or sometimes we see new php
functions from different websites. If you wanna test , you write php code on
file and save as php file and put into your web root and check via browsers.

But there is a simple way to test php functions from shell or a command line
(cmd) . What you want to do is, add the path of your php.exe in your shell/
cmd PATH variable. After that type

php -a

from command line or shell, you will get php in interactive
mode. Just type your php codes and see the result on time. No need to use a
browser or webserver to check your php code.

E:\Rework\mobshare>php
-a
Interactive mode enabled

<?php
echo ‘hai’;
hai

for($i=0;$i<10;$i++)
{
echo $i.’<br>’;
}
0<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>

PHP Dynamic Variable

August 15th, 2007 by Sajith M.R

dynamic.jpg

PHP Dynamic Variable >>
_____________________

$sajith = ‘kenney’ ;

$kenney = 110 ;

echo $$sajith ;
//will give an output 110

Another Example is:

<?php

$fashion1 = ‘fas1′;
$fashion2 = ‘fas2′;
$fashion3 = ‘fas3′;

$fashion = ”;
for($i=1; $i <= 3 ; $i++)
{
$fashion = $fashion. ‘,’ . $ { ‘fashion’ . $i } ;
}

echo $fashion;
//will give an output ,fas1,fas2,fas3

?>

PHP Swap (Simple Swapping)

August 15th, 2007 by Sajith M.R

php swapping

PHP Simple Swap >>

Hello programmers,

You know list function in php and array function. Just
type like below for swapping two variables $a and $b.

list($b,$a) = array($a,$b);

The above code is equivalent to:

$temp = $a;

$a = $b ;

$b= $temp;

Here we shorten the three line of code into a single line.

Central Align Problem in Mozilla

August 4th, 2007 by Sajith M.R

firefox.png

Alignment is a big problem in different Browsers. For example some times central
alignment of table shows correctly in IE (Internet Explorer). But shows as aligned
left in Mozilla. There is a lot of solutions for this. In mozilla you know,
for central align , there is a special string : -moz-center

eg: text-align:”-moz-center”

Similarly -moz-left, -moz-right etc are special strings for mozilla.

We can solve the alignment problem using style sheet’s auto
property.

Eg:

<style>
.test{
margin-right: auto;
margin-left: auto;
}

</style>

_________________

Add class = “test” inside a tag which is to be aligned
as center.

This will solve the central align problem in mozilla and IE

Have a nice Day
Sajith

.