Archive for category Php Coding

Php Two Ways to Write to a File

In Php you can write to a file the old fashioned way using the standard file-handling functions, fopen(), fwrite(), and fclose(). The first function opens the file and specifies the manner in which it is written to. It can specify that the function can only be read, completely rewriting the entire page, appending to the end of the page and even only writing if it is a new page. The fwrite() function simply does what the fopen() function allows it to do, and fclose() frees up the resource. Here is a basic example:

$file_to_change = “test.htm”;
$fh = fopen($file_to_change, ‘a’); //$fh stands for file handle. “a” tells us the mode = append.
if($fh === 0)
{
exit(“Could not open $file_to_change”);// Just in case. This the fopen() function will return 0 if there is a problem.
}
$info_to_write = “This is what I really want to say”;
fwrite($fh, $info_to_write);
fclose($fh);

Yes, it is three steps. Nevertheless it has some nice functionality. But there is another way. Instead of three functions, this same thing could be done with only one function:

$file_to_update=”test.htm”;
$info_to_write = “I wanted to say this too.”
file_put_contents($file_to_update, $info_to_write, FILE_APPEND | LOCK_EX);

In the above code, the FILE_APPEND serves the obvious need. LOCK_EX puts a lock on the file not allowing anyother program to write to it while this script has it open.

So why would you want to choose the first script over the second script? Well, the first script allows you to keep the script open for an extended period. Also in a very long loop the second file_put_contents is going to open and close the file unecessarily thousands perhaps millions of times.

No Comments

Php: Switch Statement

A nice way to handle conditional operations in Php is to use switch statements. This is actually more intuitive than going through a bunch of if…elseif…elseif rigmarole – although I can’t say that I haven’t done that on occasion.

Switch works by evaluating a variable once and finding a matching case in the switch. If the case is found, the appropriate code is executed. If it is not the default code is used. Here is what it looks like:

$x = “switch on”;
switch($x)
{
case “switch off”:
print “The switch is turned off.”;
break;
case
print “The switch is already on!”;
break;
default:
print “I hate wondering around in the dark.”;
break;
}

Don’t forget to use the breaks. If the breaks are not there the switch will find the case and then run all the code after it until the end of the switch and beyond. This is efficient code and works ever so slightly faster than the long string of if conditionals that we mentioned above.

No Comments

Php: Using strpos to Get Title of Webpage

When aggragating information or referring to other webpages, it can often be handy to get the title tag from a page in which you have gotten the html. The way to do this is to use the strpos() function to find the position of the title tag and the end of the tag and then extracting the title from in between. Here is an example:

$titlestart = stripos($contents, “<title>”);
$titleend = stripos($contents, “</title>”);
$stringslength = $titleend – $titlestart – 7;
$titlestart = $titlestart + 7;
$title = substr($contents,$titlestart,$stringslength);

As you can see the strpos function returns the beginning position of the string. So to find out what is AFTER the title tag, you must account for it by adding seven to the $titlestart. Then an application of the substr() function extracts the required information. The strpos() function needs a few perameters:

strpos(variable to be searched, what you are looking for)

As they say, seek and ye shall find. If the thing you are looking for is not there, then instead of returning the strings numerical position in the content the function will return “false”. And don’t forget, the first position counts as zero.

No Comments

Php: Find the Length of a String

Ah yes, another handy Php function, strlen(). I use this one alot. Besides doing yeoman’s work in string manipulation, it is great for finding out if there is anything in a variable when you know that the variable is set. Say you have passed on a $_SESSION[msg] cookie from a previous page and want to know if there is anything in it. You could do something like the following:

<?php
if(strlen($_SESSION[msg]) > 0)
{
print $_SESSION[msg];
}
?>

The output of the function is what you would expect. It turns out to be the number of characters in the string. So:

<?php
$string = “There are x number of characters.”
$num_chars_in_string = strlen($string);
print $num_chars_in_string;
//yields: 33
?>

Note that this also counts the spaces and punctuation.

No Comments

Php – Concatenation

Well, there is the Uri Nation and then there is the Concaten Nation. These are peoples that were once found in the city of Atlantis. The Uri Nation in history books now takes primary blame for the sinking of the entire continent. However the Concaten Nation had its own problems always stringing things together, they were really into empty tin can telephone lines. It tripped everyone up when it was time to head for the boats when Atlantis went for the big dive.

I know, more than you really wanted to know and not pertinent to Php. Nevertheless, concatenation is the practice of stringing things together (such as the pseudo-history above). In Php variables can be strung together using a dot as the unifying force. An example would look something like this:

<?php
$string1 = “The Concaten Nation”;
$string2 = ” got all tied up “:
$string1plus2 = $string1 . $string2 . “in a civil war.”;
print $string1plus2;
// yielding: The Concaten Nation got all tied up in a civil war.
?>

Concatenation can be a handy tool in many situations.

No Comments

Php Find Out if User Has Enabled Cookies

In working with php on my new project I needed to find out if a user had cookies enabled. I was surprised to find that there was no php function that would do this. Yet it does make sense. Php is done server side; so once it gets to the user it has done its work. Yet it can be easily done in a two step process. Simply start a session on one page and then look at the results. If the user proceeds to the next page, test a variable within the session variables to see if it exists. Of course, it must be designated on the first page.

session_start();

if(!isset($_SESSION[preset_var])) die(“To do whatever, cookies must be enabled.”);

Or send the user off to another page, using a header(), directing them to do whatever it is you want them to do.

No Comments

Php imagestring No Fontsize over 5!

At least I could not find any way to get the imagestring() function to give me a larger fontsize. On my new project, I needed to be able to write numbers and letters on an image. The problem is that I needed the numbers to be fairly large in comparison with the background. I looked high and low for a solution that was easy to implement with the fonts available to php 5.2 some odd.

In any case, I finally found a solution. I ended up having to get a true type font and uploading it to the server and then calling it using the imagettftext() function which has like eight parameters. But this actually proved much easier than I had thought. I got the font from the image website with which I have an account. The one I ended up picking out was freeware, so it and many other fonts are available free on the web with a bit of a search.

The important thing is that I got a font that looked far better than the php provided font. And with all the parameters on the imagettftext function, I can even make the text wander off at an angle. Pretty cool.

No Comments