Você está na página 1de 71

Variable Length Argument

Function

Haresh Khachariya
(Lecturer)
Shree M & N Virani Science College ,
Rajkot

func_num_args

Returns the number of arguments passed into the


current user-defined function.

Syntax:

int func_num_args ( void)


2

Example
<?php

function fnum_args()
{
$num_args = func_num_args();

echo "Number of arguments: $num_args\n";


}

fnum_args(1, 2, 3);
?>
3

func_get_arg

Returns the argument which is at the arg_num'th


offset into a user-defined function's argument list.

Function arguments are counted starting from


zero.

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

It creates and returns an array which contains


values of all arguments provided in the function
call operation.

Syntax

array func_get_args ( void)


6

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++)
{

echo "Argument $i is: " . $arg_list[$i] . "<br>\n";


}
}

fnum_args(1, 2, 3);

?>

Variable Function

gettype()

The gettype() function return the type of a


variable.

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>';

echo gettype(new stdclass());


?>
10

settype()
The

settype() function is used to set the type of


a variable.

Return

TRUE on success or FALSE on failure.

Syntax:

settype(var_name, var_type)

11

Example

$var1='98'; $var2='01'; $var3=10; $var4=true;


settype($var1, "integer");

settype($var2, "integer");

settype($var3, "string");

settype($var4, "null");

echo ($var1.'<br>');

echo ($var2.'<br>');

echo ($var1+$var2.'<br>');

echo gettype($var3)."<br>"; echo gettype($var4);

?>

Output

98

1 99 string NULL

12

isset ()

The isset () function is used to check whether a value


of variable is set or not.

The isset() function return false if testing variable


contains a NULL value or unset using unset()
function and return true if testing variable contains
values or set.

Syntax:

isset(variable1)
13

Example

<?php

$var1 = 'test'; $var2;

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

or unset the given variable.


Syntax:

unset (var1)

15

Example
<?php

$var1 = 'test'; unset($var1);


if(isset($var1))
{

echo "Var1 is set"; }

else
{

echo "Var1 is unset"; }

?>

16

strval()
The strval()

is used to convert a value of a


variable to a string.

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()

The print_r() function is used to print value of


variable or array with returns keys and elements
information.

Syntax:

print_r(var_name, return_output)

23

echo()
The echo() function outputs one or more strings.

Syntax:

echo(strings)

24

Difference Between Echo And Print_r

echo is used to output and display single types


(strings, ints, etc).

echo is a statement.

echo is a raw language construct

print_r is used to output and display the contents of an


array.

while print is a function.

print_r is functions with return values.

25

File handling Function

26

fopen()

The fopen() function is used to open files in PHP.

The first parameter of this function contains the


name of the file to be opened and the second
parameter specifies in which mode the file should
be opened.

If fopen is unable to open the file, it returns 0


(zero) otherwise it will return 1.
27

Syntax and Example

var_name=fopen(file_name,file_open_mode)

<?php
echo $file=fopen(test.txt","r") or die("can't open
file");
?>

28

The file may be opened in one of the following modes


Modes

Description

Read only. Starts at the beginning of the file

r+

Read/Write. Starts at the beginning of the file

Write only. Opens and clears the contents of


file; or creates a new file if it doesn't exist
Read/Write. Opens and clears the contents of
file; or creates a new file if it doesn't exist
Append. Opens and writes to the end of the
file or creates a new file if it doesn't exist
Read/Append. Preserves file content by
writing to the end of the file
Write only. Creates a new file. Returns
FALSE and an error if file already exists
Read/Write. Creates a new file. Returns
FALSE and an error if file already exists

w+
a
a+
x
x+

29

fwrite()

The fwrite function allows data to be written to


any type of file and an open file.

The function will stop at the end of the file or


when it reaches the specified length, whichever
comes first.

This function returns the number of bytes written


on success, or FALSE on failure.
30

Syntax and Example

var_name =fwrite(file,string,length)

<?php

$my_file = 'file.txt';

$f_open = fopen($my_file, 'w') or die("can't open


file");
$data=fwrite($f_open,"Computer Science
College");
echo $data; ?>
31

fread()

The fread() reads from an open file.

The function will stop at the end of the file or


when it reaches the specified length, whichever
comes first.

fread() function returns the read string on success,


or FALSE on failure.

32

Syntax and Example


Syntax:

$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.

fcose() function returns TRUE on success or

FALSE on failure.

34

Syntax and Example


Syntax:

fclose(file)
<?php

$f_open = fopen('file.txt', 'r') or die("can't open


file");
fclose($f_open);
?>
35

file_exists()
file_exists()

function is used to check whether a


file or directory exists or not.

Return

TRUE if the file or directory specified


by file_name exists otherwise FALSE.

36

Syntax and Example


Syntax:

var_name=file_exists (file_name)

<?php

if(file_exists('file.txt'))
{ echo "File is Found"; }

else
{
?>

echo "File isnot Found";

}
37

is_readable

The is_readable() function is used to check whether


the specified file is readable or not.

TRUE if file_name exists and is readable, FALSE


otherwise.

38

Syntax and Example

is_readable(file_name)

<?php

if(is_readable('file.txt'))

{
echo "File is Readable";

}
else
{ echo "File isnot Readable";

} ?>

39

is_writable()

The is_writable() function is used to check whether


the specified file is writeable.

TRUE if file_name exists and is writable.

40

Syntax and Example

is_writable(file_name)

<?php

if(is_writable('file.txt'))

else

?>

echo "File is Writable"; }


echo "File isnot Writable"; }

41

fgets()

The fgets() function is used to get line from file


pointer of an open file.

The fgets() function stops returning on a new line, at


the specified length, or at EOF, whichever comes
first.

This function returns FALSE on failure.

42

Syntax and Example


fgets(file_handler, byte_length)

Name

Description

file_handler

When a file is successfully opened by fopen() or


fsockopen() it returns a resource ID, which is
referred as file handler or file pointer. The fgets()
function uses this ID to return a series of bytes
from an open file

byte_length

Specifies the number of bytes to read. Default


length is 1024.

43

<?php

//$file = fopen("file.txt","r");
echo fgets($file,10);
echo fgets($file);

fclose($file);
?>

44

fgetc()

The fgetc() function reads a single character from a


open file pointed by file handler..

A string containing a single character and FALSE on


EOF.

45

Syntax and Example

fgetc(file_handler)

<?php

$file = fopen("file.txt","r");

echo fgetc($file);

fclose($file);

?>

46

file()

The file() reads whole file into an array.

Each array element contains a line from the file, with


newline still attached.

47

Syntax and Example

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

(file_name, include_path_name, context,


star_position, max_length)

49

Parameter

Description

path

Required. Specifies the file to read

include_path

Optional. Set this parameter to '1' if you want to search for


the file in the include_path (in php.ini) as well

context

Optional. Specifies the context of the file handle. Context is a


set of options that can modify the behavior of a stream. Can
be skipped by using NULL.

start

Optional. Specifies where in the file to start reading. This


parameter was added in PHP 5.1

max_length

Optional. Specifies how many bytes to read. This parameter


was added in PHP 5.1

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

Syntax and Example


Parameter

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

Optional. Specifies how to open/write to the file. Possible values:


FILE_USE_INCLUDE_PATH
FILE_APPEND
LOCK_EX

context

Optional. Specifies the context of the file handle. Context is a set of


options that can modify the behavior of a stream.

53

<?php

$str="Atmiya College Rajkot Comp. Dept.";

file_put_contents("file.txt",$str);

echo file_get_contents("file.txt");

?>

54

ftell()

The ftell() function is used to fetch the current


position of the file pointer of an open file.

ftell(file_handler)

55

Example
<?php

$file=fopen("file.txt","r");
fseek($file,"15");
echo ftell($file);

?>

56

fseek()

The fseek() function is used to move the file pointer


in an open file.

This function moves the file pointer from its current


position to a new position, forward or backward,
specified by the number of bytes.

This function returns 0 on success, or -1 on failure.

57

Syntax and Example


fseek(file,offset,whence)
Parameter

Description

file

Required. Specifies the open file to seek in

offset

The the number of bytes to move the file pointer.

whence

Optional. (added in PHP 4). Possible values:


SEEK_SET - Set position equal to offset.
Default
SEEK_CUR - Set position to current location
plus offset
SEEK_END - Set position to EOF plus offset (to
move to a position before EOF, the offset must
be a negative value)

58

<?php

$file=fopen("file.txt","r");

echo fgets($file);

echo "<br>";
echo fseek($file,"3");
echo "<br>";
echo fgets($file);

?>

Atmiya College Rajkot Comp. Dept.

0
iya College Rajkot Comp. Dept.
59

rewind()

The rewind() function is used to set the file pointer to


the beginning of the file.

This function returns TRUE on success, or FALSE on


failure.

60

Syntax and Example

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()

The copy() function copies a file from source to


destination.

This function returns TRUE on success and FALSE


on failure.

62

Syntax and Example


copy(source, destination)

<?php

echo copy("file.txt","file1.txt");

?>

63

unlink()

The unlink() function deletes a file.

This function returns TRUE on success, or FALSE on


failure.

unlink(filename)

64

Example

<?php

$file="file1.txt";

if(fopen($file,"r"))

echo "File is found";

unlink($file);

else {

echo "File isnot found";

?>

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()

The move_uploaded_file() function


uploaded file to a new location.

moves

an

This function returns TRUE on success, or FALSE on


failure.

67

Syntax
move_uploaded_file(file,newloc)

Parameter

Description

file

Required. Specifies the file to be moved

newloc

Required. Specifies the new location for the file

68

Example

$target_path = "img/".$_FILES['image1']['name'];

if(move_uploaded_file($_FILES['image1']['tmp_name'],
$target_path))

echo "File is uploaded";

}
else

?>

echo "File isnot uploaded"; }


69

<form

method="post"
enctype="multipart/form-data">

<input

type="file" name="image1">

<input

type="submit" value="Upload">

</form>

70

Questions?