Chapter 2 Function and String - BCS Guruji

Ad

Saturday, December 9, 2023

Chapter 2 Function and String

 - Explain anonymous function concept in PHP.

-What is difference between echo ( ) and print ( ) function?

Both echo and print are used to output data in PHP. The main differences are:

echo can take multiple parameters, while print can only take one.

echo is slightly faster, and print always returns 1, so it can be used in expressions.

-Explain any two directory functions.

opendir(): Opens a directory handle.

readdir(): Reads the contents of the directory handle.

-Differentiate between single quoted string and double quoted string.

Single-quoted strings ('') treat everything as plain text. Variables and escape sequences are not interpreted within single quotes.

Example: echo 'Hello, $name'; will output Hello, $name.

Double-quoted strings ("") interpret variables and escape sequences. For example, echo "Hello, $name"; will output Hello, [value of $name].

-Write any two functions of decompose string with suitable example.

explode() function:

Splits a string into an array based on a specified delimiter.

Example: $str = "apple,orange,banana"; $arr = explode(",", $str);

str_split() function:

Splits a string into an array of characters.

Example: $str = "hello"; $arr = str_split($str);

- How to find out the position of the first occurrence of a substring in a string?

 The strpos() function.

Example: $pos = strpos("Hello, world!", "world");

- What is the purpose of array_splice ( ) function?

array_splice() is used to remove a portion of the array and return it while modifying the original array.

Example: $arr = [1, 2, 3, 4]; $removed = array_splice($arr, 1, 2);

- Explain the functions used for reading and writing characters in files.

Reading: fread(), fgets(), file_get_contents()

Writing: fwrite(), file_put_contents()

 -Explain the concept of missing parameters to a function with suitable example

If a function is called with missing parameters, PHP will generate a warning or error depending on how the function is defined.

function addNumbers($num1, $num2) {

    return $num1 + $num2;

}

// Calling the function with missing parameters will result in an error

$result = addNumbers(5);

- State the use of foreach() function.
The foreach() function in PHP is used to iterate over arrays or other iterable objects, making it easy to loop through each element without the need for explicit indexing. It simplifies the process of iterating over arrays and collections.

-Give any two functions of random access of file data.

fseek():

Sets the file position indicator for the file pointer. It allows you to move the pointer to a specific position within the file.

ftell():

Returns the current position of the file pointer in a file. It is often used to determine the size of a file.

Explain any two of the following functions with syntax

 array _ intersect ()

 array _ slice ()

 shuffle ()

The array_intersect() function is used to find the intersection of two or more arrays, i.e., it returns an array containing values that are present in all input arrays.

Syntax:

$resultArray = array_intersect($array1, $array2, ...);


The array_slice() function is used to extract a portion of an array. It returns a new array containing a specified number of elements from the original array, starting at a specified offset.

Syntax:

$slicedArray = array_slice($array, $offset, $length, $preserve_keys);


The shuffle() function is used to shuffle the elements of an array randomly, changing their order.

Syntax:

shuffle($array);

- Give function to check if variable has value or not.

function isValueSet($variable) {

    return isset($variable) && !empty($variable);

}

// Example usage:

$myVar = "Hello";

if (isValueSet($myVar)) {

    echo "Variable has a value.";

} else {

    echo "Variable is empty or not set.";

}

- Write the purpose of explode function

The explode function in PHP is used to split a string into an array of substrings based on a specified delimiter. The purpose is to break a string into parts, making it easier to process or analyze individual components.

-Explain the following functions with example

 Ucwords ( )

 Trim ( )

 Str- Pad ( )

 Ucfirst  )

 Chunk - Split ( )(

ucwords() Function:

Converts the first character of each word in a string to uppercase.


$str = "hello world";

echo ucwords($str);  // Output: Hello World


trim() Function:

Removes whitespace or other characters from both ends of a string.


$str = "  Trim Me  ";

echo trim($str);  // Output: Trim Me


str_pad() Function:

Pads a string to a certain length with another string.


$str = "Hello";

echo str_pad($str, 10, "-");  // Output: Hello-----


ucfirst() Function:

Converts the first character of a string to uppercase.


$str = "capitalize";

echo ucfirst($str);  // Output: Capitalize


chunk_split() Function:

Splits a string into a series of smaller parts.


$str = "123456789";

echo chunk_split($str, 3, "-");  // Output: 123-456-789

-Explain different types of arguments passing to functions with

 example.

Pass by Value:

The actual value is passed to the function.

Pass by Reference:

The memory address of the variable is passed to the function.

-Explain Implode ( ) with suitable example.

The implode function is used to join array elements with a string. Example:

$array = array("red", "green", "blue");

$string = implode(", ", $array);

echo $string;  // Output: red, green, blue


 - Explain following functions

 fread ( )

 fwrite ( )

 fgetc ( )

 fgets ( )

fread() Function:

Reads from an open file.


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

echo fread($file, filesize("example.txt"));

fclose($file);


fwrite() Function:

Writes to an open file.


$file = fopen("example.txt", "w");

fwrite($file, "Hello, World!");

fclose($file);


fgetc() Function:

Reads a single character from an open file.


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

echo fgetc($file);

fclose($file);


fgets() Function:

Reads a line from an open file.


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

echo fgets($file);

fclose($file);

-State True/False :

 ‘‘The names of user-defined classes and functions as well as

 built-in constructs are case-insensitive.

False: In PHP, the names of user-defined classes and functions are case-insensitive, but the names of built-in constructs are case-sensitive.

- Explain the following functions with respect to a file :

 (1) filectime()

 (2) file()

 (3) stat ()

 (4) unlink()

 (5) fwrite()

filectime(): Returns the last change time of the file.

file(): Reads an entire file into an array.

stat(): Returns information about a file.

unlink(): Deletes a file.

fwrite(): Writes to an open file.

 -What is an Anonymous function ? How is it different from

 normal function ?

An anonymous function, also known as a lambda function, is a function without a name. It can be assigned to a variable or passed as an argument to other functions. Example:

$add = function ($a, $b) {

    return $a + $b;

};

echo $add(3, 4);  // Output: 7

 -How to access data members of a class inside member

 function ?

Within a class, you can access data members using $this->propertyName. For example:

class MyClass {

    public $myProperty = "Hello";


    public function printProperty() {

        echo $this->myProperty;

    }

}

$obj = new MyClass();

$obj->printProperty();  // Output: Hello


-Explain different types of arguments passing to function with example.

Pass by Value:

The actual value is passed to the function. Changes to the parameter inside the function do not affect the original value.

function increment($num) {

    $num++;

}

$value = 5;

increment($value);

// $value remains 5

Pass by Reference:

The memory address of the variable is passed to the function. Changes to the parameter inside the function affect the original value.

function incrementByReference(&$num) {

    $num++;

}

$value = 5;

incrementByReference($value);

// $value is now 6

-How to pass parameters to a function by reference ? Explain

 with example. Also write its advantage.

In PHP, you can pass parameters to a function by reference by using the ampersand (&) before the parameter in both the function definition and the function call. This allows the function to modify the original value of the variable. Here's an example:


<?php

function incrementByReference(&$num) {

    $num++;

}


$value = 10;

incrementByReference($value);

echo $value;  // Output: 11

?>

Advantage:

Passing by reference allows a function to modify the original value of a variable directly, which can be useful when you want to update a variable's value within a function.

- Explain the following functions with syntax and example :

 Func_get_arg( )

 Var_dump( )

  Strrev( )

 Similar_text( )

 Str_replace( )

-Explain the following function with example :

 Fputs( )

 Fseek( )

 readFile( )

 Filemtime( )

- ‘‘A function can have variable number of arguments.’’ State

 true or false

-How to find out the position of the first occurrence of a substring

 in a string ?


-Explain the concept of missing parameters to a function

 with suitable example.


-Explain the functions used for reading and writing characters

 in files.

- Give function to check if a variable has value or not ?

In PHP, you can use the isset function to check if a variable has a value or not. isset returns true if the variable exists and has a value other than null, and false otherwise.

$variable = 42;

if (isset($variable)) {

    echo "Variable is set and has a value.";

} else {

    echo "Variable is not set or has a value of null.";

}

In this example, the isset function checks if the variable $variable is set and has a value. If it does, it prints "Variable is set and has a value," otherwise, it prints "Variable is not set or has a value of null."


-Which function is used to check if class is present or not?

-Write any two functions of decompose string with suitable example

- What is the different between nchal and print functions.

-State the purpose of $this variable

- Write the purpose of rewind() function

-Explain the following functions with syntax and example Pune num args

1) Func_nun_args()

2)Var_dump()

3) Print_r()

4)Soundex()

5)strrpos()

 -Write an anonymous function to maximum of two numbers

- What is the use of count( ) ?

No comments:

Post a Comment