Professional Documents
Culture Documents
Function
Haresh Khachariya
(Lecturer)
Shree M & N Virani Science College ,
Rajkot
func_num_args
Syntax:
Example
<?php
function fnum_args()
{
$num_args = func_num_args();
fnum_args(1, 2, 3);
?>
3
func_get_arg
Syntax:
mixed func_get_arg ( int arg_num)
4
<?php
function fnum_args()
{
$num_args = func_num_args();
echo "Number of arguments: $num_args<br>";
if ($num_args >= 2)
{
echo "Second argument is: " . func_get_arg(1) . "<br />\n";
}
}
fnum_args(1, 2, 3); ?>
5
func_get_args
Syntax
function fnum_args()
{
$num_args = func_num_args();
echo "Number of arguments: $num_args<br>";
$arg_list = func_get_args();
for ($i = 0; $i < $num_args; $i++)
{
fnum_args(1, 2, 3);
?>
Variable Function
gettype()
Syntax:
gettype(var_name)
Example
<?php
echo gettype(102).'<br>';
echo gettype(true).'<br>';
echo gettype(' ').'<br>';
echo gettype(null).'<br>';
echo gettype(array()).'<br>';
settype()
The
Return
Syntax:
settype(var_name, var_type)
11
Example
settype($var2, "integer");
settype($var3, "string");
settype($var4, "null");
echo ($var1.'<br>');
echo ($var2.'<br>');
echo ($var1+$var2.'<br>');
?>
Output
98
1 99 string NULL
12
isset ()
Syntax:
isset(variable1)
13
Example
<?php
var_dump(isset($var1));
echo "<br>";
var_dump(isset($var2));
echo "<br>";
var_dump(isset($var2,$var3));
?> Output
bool(true)
$var3;
bool(false) bool(false)
14
unset()
The unset() function destroys a given variable
unset (var1)
15
Example
<?php
else
{
?>
16
strval()
The strval()
Syntax:
strval(var_name)
17
Example
<?php
$var_name = 22.110;
echo strval ($var_name);
?>
18
intval()
The intval() function is used to get the integer
value of a variable.
Syntax:
intval(var_name)
19
Example
<?php
$var_name = 22.110;
echo intval($var_name);
?>
20
floatval()
The floatval() function is used to convert a value
to a float.
Syntax:
floatval (var1)
21
Example
<?php
$var_name = 22.110;
echo floatval($var_name);
?>
22
print_r()
Syntax:
print_r(var_name, return_output)
23
echo()
The echo() function outputs one or more strings.
Syntax:
echo(strings)
24
echo is a statement.
25
26
fopen()
var_name=fopen(file_name,file_open_mode)
<?php
echo $file=fopen(test.txt","r") or die("can't open
file");
?>
28
Description
r+
w+
a
a+
x
x+
29
fwrite()
var_name =fwrite(file,string,length)
<?php
$my_file = 'file.txt';
fread()
32
$file_contents =fread(file,length)
<?php
$my_file = 'file.txt';
$f_open = fopen($my_file, 'r') or die("can't open
file");
$data = fread($f_open, filesize($my_file));
echo $data; ?>
33
fclose()
The fclose() function closes an open file.
FALSE on failure.
34
fclose(file)
<?php
file_exists()
file_exists()
Return
36
var_name=file_exists (file_name)
<?php
if(file_exists('file.txt'))
{ echo "File is Found"; }
else
{
?>
}
37
is_readable
38
is_readable(file_name)
<?php
if(is_readable('file.txt'))
{
echo "File is Readable";
}
else
{ echo "File isnot Readable";
} ?>
39
is_writable()
40
is_writable(file_name)
<?php
if(is_writable('file.txt'))
else
?>
41
fgets()
42
Name
Description
file_handler
byte_length
43
<?php
//$file = fopen("file.txt","r");
echo fgets($file,10);
echo fgets($file);
fclose($file);
?>
44
fgetc()
45
fgetc(file_handler)
<?php
$file = fopen("file.txt","r");
echo fgetc($file);
fclose($file);
?>
46
file()
47
file(path,include_path,context)
<?php
print_r(file("file.txt"));
?>
48
file_get_contents()
The file_get_contents() reads a whole file into a
string.
file_get_contents
49
Parameter
Description
path
include_path
context
start
max_length
50
Example
<?php
echo file_get_contents("file.txt");
?>
51
file_putcontents()
The file_put_contents() function writes a string
to a file.
file_put_contents(file,data,mode,context)
52
Description
file
Required. Specifies the file to write to. If the file does not
exist, this function will create one
Required. The data to write to the file. Can be a string, an array or a
data stream
data
mode
context
53
<?php
file_put_contents("file.txt",$str);
echo file_get_contents("file.txt");
?>
54
ftell()
ftell(file_handler)
55
Example
<?php
$file=fopen("file.txt","r");
fseek($file,"15");
echo ftell($file);
?>
56
fseek()
57
Description
file
offset
whence
58
<?php
$file=fopen("file.txt","r");
echo fgets($file);
echo "<br>";
echo fseek($file,"3");
echo "<br>";
echo fgets($file);
?>
0
iya College Rajkot Comp. Dept.
59
rewind()
60
rewind (file_handler)
<?php
$file=fopen("file.txt","r");
fseek($file,"12");
echo "<br>";
echo fgets($file);
rewind($file);
echo fgets($file);
echo "<br>";
?>
61
copy()
62
<?php
echo copy("file.txt","file1.txt");
?>
63
unlink()
unlink(filename)
64
Example
<?php
$file="file1.txt";
if(fopen($file,"r"))
unlink($file);
else {
?>
65
rename()
The rename() function renames directory or a
file.
rename(old_filename, new_filename, context)
<?php
echo rename("file.txt","newfile.txt");
?>
66
move_uploaded_file()
moves
an
67
Syntax
move_uploaded_file(file,newloc)
Parameter
Description
file
newloc
68
Example
$target_path = "img/".$_FILES['image1']['name'];
if(move_uploaded_file($_FILES['image1']['tmp_name'],
$target_path))
}
else
?>
<form
method="post"
enctype="multipart/form-data">
<input
type="file" name="image1">
<input
type="submit" value="Upload">
</form>
70
Questions?