0% found this document useful (0 votes)
113 views12 pages

PHP Unit Test Cheatsheet

This document provides a cheat sheet on PHP unit testing with 11 assignments covering PHP concepts like data types, operators, arrays, functions, and more. Key points covered include: 1. The advantages of PHP like being open source, platform independent, easy to load, stable, and user friendly. 2. The '$' sign is used to declare variables in PHP. 3. There are three pairs of opening and closing tags that can be used: <?php ?>, <? ?>, and <?php with no closing tag. 4. Examples are provided to explain variables, expressions, and common data types like string, integer, float, and boolean. 5. A foreach loop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views12 pages

PHP Unit Test Cheatsheet

This document provides a cheat sheet on PHP unit testing with 11 assignments covering PHP concepts like data types, operators, arrays, functions, and more. Key points covered include: 1. The advantages of PHP like being open source, platform independent, easy to load, stable, and user friendly. 2. The '$' sign is used to declare variables in PHP. 3. There are three pairs of opening and closing tags that can be used: <?php ?>, <? ?>, and <?php with no closing tag. 4. Examples are provided to explain variables, expressions, and common data types like string, integer, float, and boolean. 5. A foreach loop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PHP UNIT TEST CHEATSHEET :

Assignment No.1 :
1. Write any 5 advantages of PHP
A. Open Source and Free of Cost :
People can download it for free from any open source
B. Platform Independent :
PHP based application can run on any operating system like Unix,
Window and Linux
C. Easy Loading:
One can load the php based application easily and connect them to
a database
D. Stable :
Unlike other scripting language, it is very stable over the years
E. User Friendly :
It can be learned easily and implemented

2. State the use of “$” sign in php.


A. The “$” sign is used to declare a variable in PHP.
B. In php, a variable starts with “$” sign followed by a name of variable
for eg : $var_name = “Helllo World”;

3. Write the syntax of php.


A. There are 3 ways of pairs of opening and closing tags which can be
used in php
B. For opening : <?php and For closing : ?>
C. Default way:
<?php
echo “This is default”;
?>
D. Short Open Tag :
<?
echo “This is second way”;
?>
E. Omit the closing Tag :
<?php
Echo “This is third way”;

4. Explain the terms


A. Variable :
Variable in php starts with “$” sign and is assigned a value using
“=” value.
B. Expression :
In php , An expression is a line of code that has a variable and a
value.

5. Explain any four data types in php


A. String :
a) A string is a sequence of character enclosed in single or
double quotes
b) Eg:
$name_person = “Hello World”;
$name_person = ‘Hello World’
B. Integer :
a) An Integer datatype is a non-decimal number , it can either be
negative or positive.
b) Eg:
$x = 54;
$y = -54

C. Float :
a) A float is a number with decimal point
b) Eg :
$x = 3.14;
$y = 5.5;

D. Boolean :
a) A Boolean represents two positive states : TRUE or FALSE
b) Eg :
$x = true;
$y = false;

6. WAP a program using foreach loop


A. The program is :
<?php
$colors = array(“red”,”blue”,”green”,”orange”,”yellow”,”black”);
foreach($colors as $values)
{
echo “$values<br>”;
}
?>
7. List different types of operator
A. Arithmetic Operator :

Sign Name Example


+ Addition $x + $y
- Subtraction $x-$y
* Multiplication $x*$y
/ Division $x/$y

B. Comparison Operator :

Sign Name Example


== Equal $x==$y
=== Identical $x===$y
!= Not Equal $x!=$y
> Greater than $x>$y
< Lesser than $x<$y
<= Lesser than or equal $x<=$y
to
>= Greater than or $x>=$y
equal to

C. Logical Operator :

Sign Name Example


&& And $x&&$y
! Not !$x
|| Or $x||$y
Assignment No.2
1. What is array? How to store data in array?
A. In PHP, an array is a data structure that allows you to store a
collection of values under a single variable name. Each value in an
array is identified by a unique index or a key.
B. To store data in an array in PHP, you can use one of the following
methods:
a) Indexed Array:
In Indexed array, the indexes are numbers starting from zero.
To create a numeric array, you can use the following syntax:
$array = array(value1, value2, value3);
Eg :
$fruits = array("apple", "banana", "orange");

b) Associative Array:
In an associative array, the indexes are strings that you
define. To create an associative array, you can use the
following syntax:
$array = array(key1=>value1, key2=>value2,
key3=>value3);
Eg:
$ages = array("Peter"=>32, "John"=>28, "Mary"=>21);

c) Multi-dimensional Array:
In a multi-dimensional array, you can have one or more
arrays nested inside another array. To create a multi-
dimensional array, you can use the following syntax:
$array = array(array(value1, value2), array(value3, value4));
Eg :
$students = array(
array("name"=>"John", "age"=>25, "grade"=>85),
array("name"=>"Mary", "age"=>23, "grade"=>90),
array("name"=>"Peter", "age"=>27, "grade"=>75)
);

2. Explain Variable function in php.


A. Function is a block of code which is named and can be called
multiple times using that name to perform activity.
B. Variable function is basically a simple parameterized function
which can be assigned to a variable and called using the function
name or variable name.
C. Example :
<?php
function add()
{
$a = 10;
$b = 20;
echo $a+$b;
}
$x = “add”;
$x();

3. WAP to create associative array in php.


<?php
$a = array(“One”=>1, “Two”=>2, “Three”=>3);
echo $a[“One”]; ?>
4. Differentiate between implode and explode function in php
(not confirm)
Implode Explode
Converts an array to a string Converts a string to an array
Concatenates all elements of the Breaks up the string into pieces at
array with a specified delimiter a specified delimiter

Syntax: implode(delimiter, array) Syntax: explode(delimiter, string)


$colors = array('red', 'green', $color_string = 'red,green,blue';
'blue'); $colors = explode(',',
$color_string = implode(',', $color_string);
$colors); print_r($colors);
echo $color_string; // Output: Array ( [0] => red [1]
// Output: red,green,blue => green [2] => blue )

5. Define function how to define user defined function in php


A. Function is a block of code which is named and can be called
multiple times using that name to perform activity.
B. Two types of function :
a) Built-in function
b) User Defined
C. A user defined function declaration starts with the keyword
‘function’ followed by the name as the function.
D. Eg :
<?php
function display()
{
echo “This is used to display”;
}
display(); ?>
6. Write php script to sort any five number using array function
<?php
$numbers = array(7, 3, 9, 2, 5);
sort($numbers);
foreach ($numbers as $number)
{
echo $number . " ";
}
?>

7. Explain associative and indexed array with suitable example.


A. Indexed Array:
a) In Indexed array, the indexes are numbers starting from zero. To
create a numeric array,
b) you can use the following syntax:
$array = array(value1, value2, value3);
c) Eg :
$fruits = array("apple", "banana", "orange");

B. Associative Array:
a) In an associative array, the indexes are strings that you define. To
create an associative array,
b) you can use the following syntax
$array = array(key1=>value1, key2=>value2, key3=>value3);
c) Eg:
$ages = array("Peter"=>32, "John"=>28, "Mary"=>21);

8. Explain any four string function in php


A. strrev(string) :
a) reverses the string
b) eg :
$str = “Hello”;
echo strrev($str);

B. strcmp(str1,str2) :
a) This function is case sensitive returns
0 if both str1 and str2 are equal
1 if str1 is greater than str2
-1 I str2 is greater than str1
b) Eg :
$str1 = “Hello”;
$str2 = “hello”;
echo strcmp($str1,$str2);

C. strpos(string,word) :
a) Find position of a given word in a string and it is case sensitive
b) Eg :
$str = “Hello PHP”;
echo strpos($str,”PHP”);
D. str_replace($search, $replace, $subject) :
a) It is used to replace all occurrences of a string with another string
within a given string.
b) Eg :
<?php
$str = "Hello PHP";
echo str_replace("PHP","Uzumaki",$str);
?>

9. Write the syntax and use of array.flip() in php


A. PHP, the array_flip() function is used to exchange the keys and
values of an array.
B. The syntax for the array_flip() function:
array_flip($array)

10. Explain following function with examples


A. str_replace($search, $replace, $subject) :
a) It is used to replace all occurrences of a string with another string
within a given string.
b) Eg :
<?php
$str = "Hello PHP";
echo str_replace("PHP","Uzumaki",$str);
?>

B. ucwords($string) :
a) It converts the first character of each word into upper case letter in
a string
b) Eg:
<?php
echo ucwords(“hello, my name is you”);
?>

C. strlen($string) :
a) Returns the length of a string including all the white spaces and
special characters
b) Eg :
<?php
echo strlen(“Hello World”);
?>

D. strtoupper($string):
a) It is used to convert the string into upper case
b) Eg :
echo strtoupper(“hello world”);

11. Explain Extract() and Compat() function with example.


A. Extract() :
a) It is used to convert keys into variables
b) Syntax :
extract(array,extract_rules,prefix);
c) Extract rules:
EXTR_OVERWRITE
EXTR_SKIP
EXTR_PREFIX_SAME
EXTR_ALL
d) Eg :
$arr = [“One”=>1,”Two”=>2]
extract ($arr);
echo “$One,$Two”;

B. Compact() :
a) Used to convert variable to array
b) Syntax :
compact(“variable name”,”variable name”);
c) Eg :
$a = 30 ; $b = 60 ; $c=70;
$d = compact(“a”,”b”,”c”);
print-r($d);

12. State the use of str_word_count along with its syntax.


A. The str_word_count() function is used to count the number of
words in a string.
B. It is a built-in function in php
C. The syntax for the str_word_count() function:
str_word_count (string,return,char)
a) string :
specifies the string to be checked
b) return :
optional , specifies return value of the str_word_count() function
c) char :
optional . specifies special characters to be considered as words

You might also like