PHP Notes 5

PHP Notes 5 Lakshman Gupta 9:24 AM 10/8/2019

Calling the function:
my_func();
Calling the function:
$x = count_a ($name);
Calling the function:
salary_increase ($salary, 0.1);
Calling the function:
$xarr = get_values ($k, $m); // Here, $xarr becomes an array
PHP Tutorial
File System Functions:
Open File $fp = fopen(“filename”, “mode”); Open a file, where mode is either “r”,
“w” or “a”
Close File fclose ($fp);
Read a line from file $line = fgets($fp, 4096);
Write a string to a file fwrite ($fp, $string);
Read all the lines of a
file
$line = fgets($fp, 4096);
while(!feof($fp))
{ print "$line<br />\n";
$line = fgets($fp, 4096);
}
Reads line by line while NOT end-offile.
Read all the lines of a
file
$lines = file ('filename');
foreach ($lines as $line) { print $line; }
Reads all the lines of the file in an
array; then print the lines.
Delete a file unlink ('filename');
See http://www.php.net/manual/en/ref.filesystem.php for more information.
Web Application Features
One of PHP's oldest features is the ability to make HTML form and cookie data available directly to the
programmer. By default, any form entry creates a global PHP variable of the same name.
In the following example, a user name is retrieved and assigned to a variable. The name is then printed by the
sub-routine "submit.php":
<FORM ACTION="submit.php" METHOD="post">
What's your name? <INPUT NAME="myname" SIZE=3>
<br /> <input type="submit" value=" SUBMIT " name="btn_submit">
</FORM>
submit.php
<?php
print "Hello, $myname!";
?>
Working With Cookies
HTTP cookies (or just cookies) are parcels of text sent by a server to a Web client (usually a browser) and then
sent back unchanged by the client each time it accesses that server.
Each cookie has a name and value. It may also have an expiration time.
Cookies are sent by web server inside the HTTP response before any html content.
Cookies are stored in the client computer.
In PHP, to create cookie, the function setcookie is used. Example:
setcookie ("ck_cnt", $count, time()+3600*24*365); // expires in a year
In PHP, the cookies can be accessed using the super global variable $_COOKIE.
The PHP statement: extract ($_COOKIE);
creates variables with the names of cookies containing their respective values.
HTTP cookies are used for authenticating, session tracking (state maintenance), and maintaining Specific
information about users, such as site preferences or the contents of their electronic shopping carts.
The PHP function setcookie() adds headers to the HTTP response. Since headers must be inserted before the
body, all setcookie() calls must be performed before any body output (usually HTML) is printed.
The following example uses a cookie to store a form value until your browser is terminated.

<?php
if (isset($myname))
{
setcookie("myname", $myname);
}
print <<<_A_

Popular posts from this blog

Computer Fundamental In Hindi