Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Sunday, 4 April 2010

How to check if a file exists in your Web Server!

One thing I came across which was quite interesting was to check if a file exists before we do any operation on the file itself.

We do that by using a php function and we do that by:

if (file_exists('file.txt')) {
print 'OK, file.txt exists.';
}

Alright this is pretty much self-explanatory, here what we are doing is checking if the file exists or not. And if does, we simply print the message. You can simply use this function in your programs before working with the file otherwise this might throw an error if the file doesn't exists.

Very simple!

Making a String Uppercase, Lowercase, or Capitalized!!

Ok in this post I am going to show you how to Uppercase, Lowercase, or Capitalize a string or a word, I mean, making a work UPPERCASE or lowercase or Capital Word. Hmm now I guess I got things much clearer for you.

Now we'll first start with uppercase, the way we do that is:

$string = "heY hOw arE yoU doinG?";
echo strtoupper($string);
// Displays "HEY HOW ARE YOU DOING?"

What we did here is we used a PHP function called string to upper or strtoupper()

Likewise, we have the string to lower i.e strtolower()
echo strtolower($string);
// Displays "hey how are you doing?"

And yes, of course, we have the last one... called Uppercase. What this will do is uppercase everything:

$string="hoW ARE yOU doInG?";
echo ucwords(strtolower($string));
// Displays "Hey How Are You Doing?"

Important: Here, we have used a function ucwords() but before we did that we used the strtolower() function to lower case all the characters in the string... then.... we Capitalized the words in the string. Make sense?

I hope it did.

Enjoy and leave comments below!

How to check if a variable is null in PHP!

As we were talking about various functions in PHP, one of the functions to check if the value is null is: is_null($MyVal) and here if the values is null it will return true and false if the value is not null.

You can always go for something like: !is_null($MyVal) that way, if the value is not null then do something.

Very simple and straight forward!

Ok again, leave your comment down below.

Cheers!