PHP
Defining class in PHP
by Binod on Sep.19, 2011, under PHP, Technology, Web Designing
This is a simple Example of Defining a class in PHP.
<?php
class test
{
function do_test()
{
echo “Testing class function.”;
}
}$test_object = new test;
$test_object->do_test();
?>
The Output will be
Testing class function.
E-mail Script in PHP
by Binod on Sep.18, 2011, under PHP, Web Designing
This is an example of simple e-mail script for PHP that can be used.
<?php
//define the receiver of the email
$to = ‘someone@somemail.com’;
//define the subject of the email
$subject = ‘Test email’;
//define the message to be sent. Each line should be separated with \n
$message = “Hello World!\n\nThis is my first mail.”;
//define the headers we want passed. Note that they are separated with \r\n
$headers = “From: webmaster@example.com\r\nReply-To: webmaster@example.com”;
//send the email
$mail_sent = mail( $to, $subject, $message, $headers );
//if the message is sent successfully print “Mail sent”. Otherwise print “Mail failed”
if($mail_sent){
print “Mail sent”;
}
else {
print “Mail failed.”;
}?>
Write your sentence in reverse order [string manipulation]
by Binod on Sep.17, 2011, under PHP, Tips & Tricks
Its time for fun. after writing few blogs about PHP basics syntax i wished to play with strings. I want to Display the sentence in a reverse order.
Suppose i have a sentence “ Hello Binod How Are you” i wish to display it as “you Are How Binod Hello”
Enjoy the Code. Here it goes.
<?php
$str = “Hello Binod How Are you”;
$str_arr = explode(” “, $str);
$str_rev = array_reverse($str_arr);
foreach($str_rev as $word){
echo $word;
echo ” “;
}?>
Date() in PHP
by Binod on Sep.15, 2011, under PHP, Technology
This is a simple example of printing the date in different format that can be applicable for various purposes.
<?php
// Assuming today is: September 10th, 2011, 5:16:18 pm$today = date(“F j, Y, g:i a”); // September 10, 2011, 2:34 am
echo “<br>”.$today;
$today = date(“m.d.y”); // 09.10.11
echo “<br>”.$today;
$today = date(“j, n, Y”); // 10, 9, 2011
echo “<br>”.$today;
$today = date(“Ymd”); // 20110910
echo “<br>”.$today;
$today = date(‘h-i-s, j-m-y, it is w Day z ‘); // 02-34-56, 10-09-11, 3430 3456 6 Satam11 252
echo “<br>”.$today;
$today = date(‘\i\t \i\s \t\h\e jS \d\a\y.’); // It is the 10th day.
echo “<br>”.$today;
$today = date(“D M j G:i:s T Y”); // Sat Sep 10 2:34:56 NPT 2011
echo “<br>”.$today;
$today = date(‘H:m:s \m \i\s\ \m\o\n\t\h’); // 02:09:56 m is month
echo “<br>”.$today;
$today = date(“H:i:s”); // 02:34:56
echo “<br>”.$today;
?>
The Output will be as
September 10, 2011, 2:34 am
09.10.11
10, 9, 2011
20110910
02-34-56, 10-09-11, 3430 3456 6 Satam11 252
it is the 10th day.
Sat Sep 10 2:34:56 NPT 2011
02:09:56 m is month
02:34:56
Useful string functions for web development
by Binod on Sep.14, 2011, under PHP, Technology, Tips & Tricks
Here i have shared some useful string manipulation functions that can be very important in website development. I had not explained it but the explanation code is all here. i guess the example will explain itself. if you need any help feel free to comment.
<?php
$string_a = “This is a long string”;
$string_b = “This is a much longer string”;
$explode_arr = explode(” “, $string_a);
print “<br>”.$explode_arr[3];// will pirnt long
$string_n = “This is line 1.\nThis is line 2″;
print “<br>”.nl2br($string_n);
$result = strcmp($string_a, $string_b);//can be useful for password comparision
if($result == 0){
print “<br>”.”String A and B are equal”;
}
elseif($result < 0){
print “<br>”.”String A is less than String B.”; //this is printed
}
elseif($result > 0 ) {
print “<br>”.”String A is greater than String B.”;
}
$length = strlen ($string_b);
print “<br><br>”.”String length of string ‘$string_b’ is $length.”;
print “<br><br>”.”Lower case of string $string_a is -> “.strtolower($string_a).”.”;
print “<br><br>”.”Uppder case of string $string_a is -> “.strtoupper($string_a).”.”;
$sub_string_1 = substr ($string_b, -6);
print “<br><br>Substring 1 “.$sub_string_1;
$sub_string_2 = substr ($string_b, 0, 7);
print “<br><br>Substring 2 “.$sub_string_2;
//try some substring variations yourself
$string_t = “ Binod Ranabhat “;
print “<br><br>String lenghth without trimming -”.strlen($string_t).”- and String length after trim -”.strlen(trim($string_t)).”-.”;
//try ltrim and r trim youself.
?>
The output will be as
long
This is line 1.
This is line 2
String A is less than String B.
String length of string ‘This is a much longer string’ is 28.
Lower case of string This is a long string is -> this is a long string.
Uppder case of string This is a long string is -> THIS IS A LONG STRING.
Substring 1 string
Substring 2 This is
String lenghth without trimming -21- and String length after trim -14-.
foreach in PHP
by Binod on Sep.13, 2011, under PHP, Technology
This is a simple example of playing with an array and a for loop with keyword FOREACH in PHP.
<?php
$arr = array(“zero” , “one” , “two” , “three” , “four” );
/*For each example 1 */
foreach ( $arr as $value ){
print “<br>”.$value;
}
print “<br>”;
/*For each example 2 */
foreach ( $arr as $key => $value) {
print “<br>At key “.$key.” of the array, the value is “.$value;
}
?>
The Output will be
zero
one
two
three
fourAt key 0 of the array, the value is zero
At key 1 of the array, the value is one
At key 2 of the array, the value is two
At key 3 of the array, the value is three
At key 4 of the array, the value is four
Defining a number Format in PHP
by Binod on Sep.12, 2011, under PHP, Technology
This is a simple example of defining the number format in PHP
<?php
$a = 10;
$b = 250;
$tot = $a*$b;
echo “Rs. “.number_format($tot, 2);
?>
The Output will be as a formatted number with comma ‘,’ and ‘.00’
Rs. 25,00.00
Post Increment and Pre increment
by Binod on Sep.10, 2011, under PHP, Tips & Tricks
Most of the Programmer along with me are confused in Post Increment/decrement and Pre Increment/decrement of the Operator. I am here posting an example of Post Increment and Pre increment done in PHP. This must help us all.
<?php
$a = 5;
echo “<h2>A initially is “.$a.”.</h2>”;
echo “<h3>Postincrement</h3>”;
echo “Should be 5: ” . $a++ . “<br />\n”;
echo “Should be 6: ” . $a . “<br />\n”;
echo “<h3>Preincrement</h3>”;
$a = 5;
echo “A is reset to: “.$a. “<br />\n”;
echo “Should be 6: ” . ++$a . “<br />\n”;
echo “Should be 6: ” . $a . “<br />\n”;
echo “<h3>Postdecrement</h3>”;
$a = 5;
echo “A is reset to: “.$a. “<br />\n”;
echo “Should be 5: ” . $a– . “<br />\n”;
echo “Should be 4: ” . $a . “<br />\n”;
echo “<h3>Predecrement</h3>”;
$a = 5;
echo “A is reset to: “.$a. “<br />\n”;
echo “Should be 4: ” . –$a . “<br />\n”;
echo “Should be 4: ” . $a . “<br />\n”;
?>
The Output will be
A initially is 5.
Postincrement
Should be 5: 5
Should be 6: 6
Preincrement
A is reset to: 5
Should be 6: 6
Should be 6: 6
Postdecrement
A is reset to: 5
Should be 5: 5
Should be 4: 4
Predecrement
A is reset to: 5
Should be 4: 4
Should be 4: 4
Passing JavaScript variables to PHP
by Binod on Aug.28, 2011, under Javascript, PHP, Tips & Tricks, Tutorial, Web Designing
What if I can pass variables between 2 scripting language. JavaScript is mainly used as a client side scripting language, while PHP is a server side technology. Unlike Java or ASP.Net, PHP doesn’t have tools to make it work client side. That is why you need to combine JavaScript and PHP scripts to develop powerful web-applications.
One of the frequent problems is defining visitor’s screen resolution using JavaScript tools and passing this data to PHP-script. The following script provides solution for this problem:
<!—index.htm –>
<script type=”text/javascript”>
width = screen.width;
height = screen.height;
if (width > 0 && height >0) {
window.location.href = “http://localhost/main.php?width=” + width + “&height=” + height;
} else
exit();
</script>
Copy and paste this code snippet in the text editor, save it as index.htm and run it in your browser. After this code has been executed, a user is automatically redirected to the main.php page where screen resolution is displayed in the browser window.
The main.php looks as follows:
<!—main.php –>
<?php
echo “<h1>Screen Resolution:</h1>”;
echo “Width : “.$_GET['width'].”<br>”;
echo “Height : “.$_GET['height'].”<br>”;
?>
As you can see, passing JavaScript variables in PHP is similar to sending data using GET method.
Get File Extension in PHP
by Binod on Aug.28, 2011, under PHP, Tips & Tricks, Tutorial, Web Designing
Today I will be sharing some piece of code for getting the real file extension of the uploaded file. I was working on the CMS few days back and there I made the file upload section but I didn’t checked which type of file is been uploaded. During my testing phase I just tested for jpeg file.[ as it was my target]. I hosted it over my server, it was working fine…
The next day, I received a wall post on Facebook from my friend. Binod, you got a security flaw in ur script. I checked it. What he did was he uploaded his scripted file [PHP file] thru that upload section and started to traverse my whole server files.
Then, I realized I need to check the extension of the file and to block the unwanted extension. (E.g. “php” from “myscript.php”).
Here is some piece of code of my CMS that will be beneficial for the users.
//for the upload page
<input type=”file” name=”myfile” />
//for the submit page
$fileatt = $_FILES['myfile']['tmp_name'];
$filename = $_FILES['myfile']['name'];//there are Five ways that may be helpful
// 1. The “explode/end” approach
$ext = end(explode(‘.’, $filename));// 2. The “strrchr” approach
$ext = substr(strrchr($filename, ‘.’), 1);// 3. The “strrpos” approach
$ext = substr($filename, strrpos($filename, ‘.’) + 1);// 4. The “preg_replace” approach
$ext = preg_replace(‘/^.*\.([^.]+)$/D’, ‘$1′, $filename);//5. The “pathinfo” approch
$ext = pathinfo($filename, PATHINFO_EXTENSION);//Checking The uploaded
if(($ext==”jpg”)||($ext==”jepg”)||($ext==”gif”)||($ext==”png”)){
// Uploading and processing script
}else{
// Error script amd msg
}