You are on page 1of 3

Program + Semester Course Name Section Code

CET IV Internet Programming IN4074-G3


Percentage Weight of Total
Type of Evaluation
Fall 2022 Evaluation
Assignment # 2-1
5%
Course Instructor
Due Date
Muhammad Waqar
Week 8 (Oct 30th, 2022) Total Marks: /5
Aslam

Student Name: ______________________________________ Student ID #: ________________

Note: Please write proper citations. Assignment without citations will not be acceptable. Please use IEEE
style referencing.

Question No. 1 (Marks 1)

What is the difference between a double quoted and a single quoted string in PHP? Briefly
describe.
ANS. Single quoted strings will display things almost completely "as is." Variables and most
escape sequences will not be interpreted. The exception is that to display a literal single quote,
you can escape it with a back slash \', and to display a back slash, you can escape it with another
backslash \\ (So yes, even single quoted strings are parsed).

Double quote strings will display a host of escaped characters (including some regexes), and
variables in the strings will be evaluated. An important point here is that you can use curly
braces to isolate the name of the variable you want evaluated. For example let's say you have
the variable $type and you want to echo "The $types are". That will look for the variable $types.
To get around this use echo "The {$type}s are" You can put the left brace before or after the
dollar sign. Take a look at string parsing to see how to use array variables and such. (Mannhardt,
2019)

Question No. 2 (Marks 1)

Explain the difference between a heredoc and nowdoc.


ANS. Heredoc and nowdoc both are two methods for defining strings in PHP 5.3.

The key difference between them is Heredoc parses for variable interpolation whereas Nowdoc
doesn't do the same. In other words, no parsing is done inside a Nowdoc. (Majmudar)

Question No. 3 (Marks 1)

Explain the purpose of htmlentities function in PHP.

Page 1 of 3
ANS. The htmlentities is used to convert all applicable characters to HTML entities.

Syntax
htmlentities(str,flags,character-set,double_encode)
Parameters
str − The string to convert.

flags −How to handle quotes, invalid encoding and the used document type.

The following are the available quote styles −

ENT_COMPAT − Default. Encodes only double quotes

ENT_QUOTES − Encodes double and single quotes

ENT_NOQUOTES − Does not encode any quotes


(https://www.tutorialspoint.com/htmlentities-function-in-php, 2020)

Question No. 4 (Marks 1)


How do we use implode and explode functions for strings?

Ans. There are some situations where we need to convert a string to array and vice versa to
work with our logic, here implode and explode functions comes in handy. It’s easy to learn and
implement.

The implode function implodes an array into a string, and the explode function explodes a string
into an array. It’s very useful if you’ve stored your data in strings in files and want to convert
those strings into array elements when your Web application runs.

Let’s take a look at an example below:

<?php

$flower[0]=”Lily”;
$flower[1]=”Rose”;
$flower[2]=”Tulip”;
$text=implode(“, “,$flower);
echo $text;

?> (PHP implode and explode functions for string and arrays, 2015)

Question No. 5 (Marks 1)

Page 2 of 3
What are two different ways to convert a string into a number? Give examples.
Ans. Use Number() function, which converts the object argument to a number that represents
the object's value.

Example
The given example will return 384.75

https://www.tutorialspoint.com/htmlentities-function-in-php. (2020).

Majmudar, R. (n.d.).

Mannhardt, M. (2019).

PHP implode and explode functions for string and arrays. (2015).

Page 3 of 3

You might also like