Developers Archive for the 'php function explanations' Category

Str_pad function

Str_pad function Wednesday, February 28th, 2007

Syntax:

string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])

str_pad function is used to pad a string to specific length with another string.

Example:

<?php

$username = “sampleuser”;

echo str_pad($username,18,’*',STR_PAD_BOTH);

?>

In above example, First parameter $username is input string. Second parameter 18 is pad length. Third parameter * is the pad string which is optional. Default pad string is space. Fourth parameter STR_PAD_BOTH is pad type which will pad string on both sides. Default pad type is STR_PAD_RIGHT.

tmpfile() function in php

tmpfile() function in php Tuesday, February 27th, 2007

- The best way to create the temporary file is with the tmpfile()
function. This function creates a temporary file with a unique random name and opens this for writing.
- And also temporary file will be closed while we use fclose() function.

Example
$fp = tmpfile();
$data = “Test test test test”;
fwrite($fp, $data);
fclose(fp);

Get user input on the command line (Windows only)

Get user input on the command line (Windows only) Thursday, February 15th, 2007

There is a code to get user input on ‘*nix’, now there is a code to do the same on Windows based system. Put this code in a file, for example input.php and run it with ‘php -f input.php’

<?php

function read() {
$fp=fopen(”con”, “rb”);
$input=fgets($fp, 255);
fclose($fp);
return str_replace(”rn”, “”, $input);

}

echo(”Whats your name? “);
$name = read();
echo(”Hello $name!n”);

?>


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.