Showing posts with label PHP for Beginner. Show all posts
Showing posts with label PHP for Beginner. Show all posts

Sunday, June 22, 2008

PHP Round Off Decimal or Convert to Decimal Point

Round Off Decimal

Round a number up or down or Round off a floating decimal point number using PHP's functions round().

Example. round();


echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
?>

Convert to Decimal Point

Example. number_format();


$number = "28";
$number = number_format($number, 2);
echo $number; // equals "28.00"
?>

Tuesday, April 29, 2008

MySQL Connection using PHP Script:

MySQL Connection using PHP Script:

PHP provides mysql_connect() function to open a database connection. This function takes five parameters and returns a MySQL link identifier on success, or FALSE on failure.

Syntax:

connection mysql_connect(server,user,passwd,new_link,client_flag);

You can disconnect from MySQL database anytime using another PHP function mysql_close(). This function takes a single parameter which is a connection returned by mysql_connect() function.

Syntax:

bool mysql_close ( resource $link_identifier );


Example:
<html> 
<head>
<title>Connecting MySQL Server</title>
</head>
<body>
<?php $dbhost = 'localhost:3036';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

mysql_close($conn);?>

</body></html>

Sunday, March 16, 2008

PHP Comparison Operators

Comparison Operator are use to compare between 2 variable. It will return true of false value.

Below are the common comparison operator use like other languages use.

Operator

Description

==

is equal to

!=

is not equal

>

is greater than

<

is less than

>=

is greater than or equal to

<=

is less than or equal to

PHP Arithmetic Operator

Below are the Arithmetic Operator use in PHP:

Operator

Description

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus (division remainder)

++

Increment

--

Decrement

PHP Comment

In PHP, // is use to make a single-line comment or /* and */ to make a large comment block.

Examples:

<html>
<body>

<?php

//Single comment use //

/*
Block
comment
*/

?>

</body>
</html>

PHP Basic

PHP is a Server Side Scripting. It is embeded into the HTML code.

Basic code:

<html>
<?php echo "Hello World" ?>
</html>

The PHP code begin with the tag. The output of the code is Hellow World.