PHPH
PHPH
// code to be executed
while loop loops through a block of code as long as the specified condition is
true.The while loop executes a block of code repeatedly until the condition is false.
Once the condition gets false, it exists from the body of loop.
While (if the condition is true) {
// code is executed
do-while loop loops through a block of code once, and then repeats the loop as long
as the specified condition is true. A statement is executed at least once on using the
do…while loop. After executing once, the program is executed as long as the
condition holds true.
Static WebsitevsDynamic Website
Content of Web pages can not be change at runtime. Content of Web pages can be
changed.No interaction with database possible. Interaction with database is
possibleIt is faster to load as compared to dynamic website.It is slower than static
website.Cheaper Development costs. More Development costs.
No feature of Content Management. Feature of Content Management System.
HTML, CSS, Javascript is used for developing the website. Server side languages such
as PHP, Node.js are used.
Same content is delivered everytime the page is loaded. Content may change
everytime the page is loaded
UNIT2. A function is a block of code written in a program to perform some specific
task. We can relate functions in programs to employees in a office in real life for a
better understanding of how functions work. Suppose the boss wants his employee
to calculate the annual budget. So how will this process complete? The employee will
take information about the statistics from the boss, performs calculations and
calculate the budget and shows the result to his boss. Functions works in a similar
manner. They take informations as parameter, executes a block of statements or
perform operations on this parameters and returns the result.
PHP provides us with two major types of functions:Built-in functions : PHP provides
us with huge collection of built-in library functions. These functions are already
coded and stored in form of functions. To use those we just need to call them as per
our requirement like, var_dump, fopen(), print_r(), gettype() and so on.User Defined
Functions : Apart from the built-in functions, PHP allows us to create our own
customised functions called the user-defined functions.
Using this we can create our own packages of code and use it wherever necessary by
simply calling it.
Creating a Function
Any name ending with an open and closed parenthesis is a function.A function name
always begins with the keyword function.To call a function we just need to write its
name followed by the parenthesis.A function name cannot start with a number. It
can start with an alphabet or underscore.A function name is not case-sensitive.
function function_name(){
executable code;
}
Q.Reusability: If we have a common code that we would like to use at various parts
of a program, we can simply contain it within a function and call it whenever
required. This reduces the time and effort of repetition of a single code. This can be
done both within a program and also by importing the PHP file, containing the
function, in some other program
Easier error detection: Since, our code is divided into functions, we can easily detect
in which function, the error could lie and fix them fast and easily.
Easily maintained: As we have used functions in our program, so if anything or any
line of code needs to be changed, we can easily change it inside the function and the
change will be reflected everywhere, where the function is called. Hence, easy to
maintain.
Q.A recursive function is a function that solves a problem by solving smaller
instances of the same problem. This technique is often used in programming to solve
problems that can be broken down into simpler, similar subproblems.
Components of a recursive function:
Base case: Every recursive function must have a base case. The base case is the
simplest scenario that does not require further recursion. This is a termination
condition that prevents the function from calling itself indefinitely. Without a proper
base case, a recursive function can lead to infinite recursion.
Recursive case: In the recursive case, the function calls itself with the modified
arguments. This is the essence of recursion – solving a larger problem by breaking it
down into smaller instances of the same problem. The recursive case should move
closer to the base case with each iteration.
formal arguments:Formal arguments are the variables defined in the function's
declaration or definition.tThey act as placeholders that receive values when the
function is called.Actual arguments;Actual arguments are the values or variables
passed to the function when it is invoked.
These values are assigned to the formal arguments.
String can also be defined as a sequence of characters, stored in contiguous memory
locations, terminated by a special character called the null character ‘\0’.
Strings are widely used in computer science and have many applications in various
fields, some of which are:
Text Processing: Strings are used to represent and manipulate text data, such as in
text editors, word processors, and other applications that deal with text.
Pattern Matching: Strings can be searched for patterns, such as regular expressions
or specific sub-strings, to extract or process data in a specific way.
Data Compression: Strings can be compressed to reduce the amount of storage
required to store them. String compression algorithms, such as Huffman coding and
run-length encoding, are commonly used in data compression applications.
Q.Library function in PHP are built-in functions provided by the language to perform
various tasks, such as string manipulation, array operations, file handling, mathematical
calculations, and more. Here are some commonly used library functions in PHP:
strlen(): Returns the length of a string echo strlen output("Hello, PHP!"); // Output: 10
Array FunctionsThese functions allow manipulation of arrays.count(): Returns the
number of elements in an arraphp
Copy code)
$arr = [1, 2, 3];
echo count($arr); // Output: 3
File Handling Functions
These functions help in reading, writing, and managing files.fopen(): Opens a file for
reading, writing, or appending.
php
$file = fopen("example.txt", "r");
. UNIT-4
permissions is at the core of Unix/Linux file system. The permissions
determine who can access a file and how one can access a file. File
permissions in Linux are manipulated by the chmod command, which can
be run inside the Linux terminal. PHP provides the chmod() function with
which you can handle file permissions programmatically.
PHP’s chmod() function is effective only when you are working on a Linux OS.
It doesn’t work on Windows, as Windows OS has a different mechanism of
controlling file permissions.Types of File Permissions
The open() function is used to open a file in Python. Its syntax is: file_object =
open(file_name, mode)
When discussing file opening and closing in Python, particularly using the open() function,
it is crucial to understand the basics of file handling and how to manage resources efficiently.
Python provides various ways to handle files, including manual opening and closing or using
context managers to automate resource management.
The open() function is used to open a file in Python. Its syntax is:
python
Copy code
file_object = open(file_name, mode)
Example:
python
Copy code
file = open("example.txt", "r")
content = file.read() # Read file content
2. File Close
After performing operations on a file, it is essential to close it to free system resources. Use
the close() meth
Failing to close a file can lead to resource leaks and potential issues when accessing the file
again.