class in php

Tuesday, November 18, 2008 9:32
Posted in category PHP

Class is nothing but binding the data. I’e variables and functions. In class we can have number functions. Class can be declare with a key word class followed by a class name, which can be any name. This name should not be key word in php. Followed by a pair of curly braces .
Its recommended to start class name with a capital letter. And functions with a small letter. Before class declarations we can use modifiers I’e public, abstract. If access modifier is nothing by default it will take defaultft modifier..

public class Demo{

$i=0;

function myfristfunction(){

$i++;

echo $i;

echo "this my first function ";

}

function mysecondfunctin(){

$i++;

echo $i;

echo "mysecond function";

}

}

output:
1 this my first function 2 mysecond function

class Second{

function mysecondclass(){

echo "this is my second class";
}

}

output:

this is my second class

trim() in php

Tuesday, November 11, 2008 10:53
Posted in category PHP

If you use a form on your site then you’ll know that user input can often have white space before or after the text as the person filling in the form field sometimes accidentally adds spaces. This handy function will remove the whitespace from the beginning or end of the string.



< ?php

echo trim($_REQUEST['name']);

?> 

after removing the white spaces

out put :hai

< ?php
$value=" Hello world ";
function trim_value($value) {
echo $value = trim($value);
}
?> 

out put:

Hello world

Using curl functions in php to send sms for smaple sms gateway

Saturday, November 8, 2008 11:42
Posted in category PHP, Programming

This is a sample code assuming we have a sms gateway with name smsgateway.co.cc. We have take a sample

$curlPost = 'Mobile_No=9000099119&Type=SMS&Message="This is a test message"';

//we have to pass the mobile number and message as argument to the url. 

$LOGINURL = "http://smsgateway.co.cc/UserMiscellaneousMT.asp"; 

//This url will be provided by our gateway 

$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";

//setting the browser agent as Firefox 

$ch = curl_init(); 

// initiate a curl function c

url_setopt($ch, CURLOPT_URL,$LOGINURL); 

//set the url to be used 

curl_setopt($ch, CURLOPT_USERAGENT, $agent);

//set the browser 

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2); 

//set the maxmium time to execute 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// accept the responce after the execution 

curl_setopt($ch, CURLOPT_POST, 1); 

// set the post method for passing variables to the server 

curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);

//assigning the post values 

$result = curl_exec ($ch);

//executing with the above set options and catching the values in a variable 

curl_close ($ch); 

// closing the connection 

echo $result;

// printing the result.
CURL_INIT():-

The curl_init() function returns a Curl instance for us to use in later functions, and you should always store it for later. It has just one optional parameter: if you pass a string into curl_init(), it will automatically use that string as the URL to work with. In the script above, we use curl_setopt() to do that for clarity, but it is all the same.

Curl_setopt():-

Curl_setopt() takes three parameters, which are the Curl instance to use, a constant value for the setting you want to change, and the value you want to use for that setting. There are a huge number of constants you can use for settings, and many of these are listed shortly. In the example we use CURLOPT_URL:- which is used to set the URL for Curl. CURLOPT_USERAGENT:- which is used to set browseragent for Curl. CURLOPT_CONNECTTIMEOUT:- which is used to set the MAX ececution tiem for Curl. CURLOPT_RETURNTRANSFER:- which is used to set the Returntransfer enable for Curl. CURLOPT_POST:- which is used to set the method of posting the variables for Curl. CURLOPT_POSTFIELDS:- which is used to set the variables for Curl.

curl_exec():-

Calling curl_exec() means, We finished setting our options and ready do it, and you need to pass precisely one parameter: the Curl resource to use. The return value of curl_exec() is true/false by default.

curl_close():-

The final function, curl_close(), takes a Curl resource as its only parameter, closes the Curl session, then frees up the associated memory.

count() in php

Saturday, November 8, 2008 11:29
Posted in category PHP, Programming

The count() function is useful when you want to return how many elements are in your array. You can then use this in a for loop. Here’s an example we used earlier, only this time with the count function:

$a[0]  = 2;
$a[5]  = 3;
$a[10] = 6;
$totalsize = count($a);

for($i=o;$i< $totalsize;$i++){

echo $totalsize[$i];

}

round () in php

Wednesday, November 5, 2008 23:41
Posted in category PHP, Programming

The PHP function round () is used to round numbers up or down based on their value (Any decimal .5 or above is rounded up, anything under .5 is rounded down.)

by default it will round to the nearest whole number

< ?php
echo (round(0.70) . "
"); echo (round(0.50) . ""); echo (round(0.20) . ""); echo (round(-3.40) . ""); ?>

out put
1
1
0
-3