Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Friday, July 1, 2016

PHP Unlink Function

Remember from the PHP File Create lesson that we created a file named testFile.txt.

PHP Code:

$myFile = "testFile.txt";
$fh = fopen($myFile, w) or die("cant open file");
fclose($fh);

Now to delete testFile.txt we simply run a PHP script that is located in the same directory. Unlink just needs to know the name of the file to start working its destructive magic.

PHP Code:

$myFile = "testFile.txt";
unlink($myFile);

The testFile.txt should now be removed.

PHP - Unlink: Safety First!

With great power comes a slough of potential things you can mess up! When you are performing the unlink function be sure that you are deleting the right file!

Saturday, June 18, 2016

Using the static Statement to Remember the Value of a Variable Between Function Calls


If you declare a variable within a function in conjunction with the static statement, the variable remains local to the function, and the function "remembers" the value of the variable from execution to execution





<?php
function numberedHeading($txt) {
static $num_of_calls = 0;
$num_of_calls++;
echo "<h1>".$num_of_calls." ". $txt."</h1>"; }
numberedHeading("Mobile Phones");
echo "<p>We build a fine range of mobile phones.</p>";
numberedHeading("Camera");
echo "<p>Also Digital Cameras..</p>";
?>

The numberedHeading() function has become entirely self-contained. When we declare the $num_of_calls variable on line 3, we assign an initial value to it. This assignment is made when the function is first called on line 7. This initial assignment is ignored when the function is called a second time on line 9. Instead, the code remembers the previous value of $num_of_calls. We can now paste the numberedHeading() function into other scripts without worrying about global variables.

Thursday, June 9, 2016

Saving State Between Function Calls with the static Statement

Local variables within functions have a short but happy life they come into being when the function is called and die when execution is finished, as they should. Occasionally, however, you may want to give a function a rudimentary memory.
Lets assume that we want a function to keep track of the number of times it has been called so that numbered headings can be created by a script. We could, of course, use the global statement to do this, as shown in Listing below:
<?php
$num_of_calls = 0;
function numberedHeading($txt) {
global $num_of_calls;
$num_of_calls++;
echo "<h1>".$num_of_calls." ".$txt."</h1>";
}
numberedHeading("Mobile Phones");
echo "<p>We build a fine range of mobile phones.</p>";
numberedHeading("Camera");
echo "<p>Also Digital Cameras.</p>";
?>


Sunday, May 29, 2016

Returning User Defined Function

A function can return a value using the return statement in conjunction with a value. The return statement stops the execution of the function and sends the value back to the calling code.

Example: creates a function that returns the sum of two numbers.

<?php
function multiply($firstnum, $secondnum) {
$result = $firstnum * $secondnum;
return $result;
}
echo multiply(9,5);
//will print "45"
?>

Put these lines into a text file called multiply.php and place this file in your web server document root. When you access this script through your web browser, it produces the following:
45

Notice in line 2 that multiply() should be called with two numeric arguments (line 6 shows those to be 9 and 5 in this case). These values are stored in the variables $firstnum and $secondnum. Predictably, multiply() multiplies the numbers contained in these variables and stores the result in a variable called $result.

The return statement can return a value or nothing at all. How we arrive at a value passed by return can vary. The value can be hard-coded:
return 4;

It can be the result of an expression:
return $a/$b;

It can be the value returned by yet another function call:
return another_function($an_argument);


Monday, May 16, 2016

Date Format Function Change Date Format In PHP

Many People have problem in converting between various date formats in PHP.So I have discussed some basic solutions to some common date related problems in this post.

Converting Date Format From dd-mm-yyyy to yyyy-mm-dd
echo date("Y-m-d",strtotime($time));
Converting Date Format From yyyy-mm-dd to dd-mm-yyyy
$new_date = date(d-m-Y, strtotime(your date variable));
Changing Date Format From Full Month Name
date("Y-m-d",strtotime("Your Date Variable"));
 For Example,
date("Y-m-d",strtotime("15 November, 2012"));
 Returns 2012-11-15.

Hope this solves some of yours Date related problems in PHP.

If you like this php script, you can Subscribe below to receive all our future updates directly in your email for free.

Cheers.
Have A Great Day!

Sunday, May 8, 2016

function

PHP has many built-in functions. For example, strtoupper(), this function is to convert string to uppercase, another example is abs(), which is to return a value to be absolute. 
But we can also define our own function using statement:

function name_of_function($argument1, $argument2) 
{
 //function code here
}

The name of the function follows the function statement and precedes a set of parentheses. If your function requires arguments, you must place comma-separated variable names within the parentheses. These variables will be filled by the values passed to your function. Even if your function doesnt require arguments, you must nevertheless supply the parentheses.
By the way, The naming rules for functions are similar to the naming rules for variables, "The Building Blocks of PHP." Names cannot include spaces, and they must begin with a letter or an underscore. As with variables, your function names should be meaningful as well as consistent in style. The capitalization of function names is one such stylistic touch you can add to your code; using mixed case in names, such as myFunction() or handleSomeDifficultTask(), makes your code much easier to read.
see the example below:

<?php
function bold($word){
print("<b>$word</b>");
}

function italic($word) {
print("<i>$word</i>");
}

function underline($word) {
print("<u>$word</u>");
}

bold("Good Morning...");echo("<br/>");
italic("Good Afternoon...");echo("<br/>");
underline("Good Evening...");echo("<br/>");
?>
save it as function.php and open from your browser, the result should be like;

Good Morning...
Good Afternoon...
Good Evening...

all the  functions above expect a string as a paramater, so we place the variable name $word between the parentheses when we declare the function. Whatever is passed to all functions will be stored in this $word variable. Within the body of the functions,we print the $word variable, appending a <b>...</b> element to "bold function", <i>...</i> to "italic function" and <u>...</u> to "underline function".