0% found this document useful (0 votes)
27 views11 pages

PHP Notes

Uploaded by

memedec471
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views11 pages

PHP Notes

Uploaded by

memedec471
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

PHP NOTES

1)What is PHP?
Ans: PHP, which stands for Hypertext Preprocessor, is a widely
used server-side scripting language designed for web
development. It is particularly well-suited for creating dynamic
web pages and can be embedded within HTML code.

2)What is Difference Between “Echo” and “Print”?


Ans: PHP, both echo and print are used to output data to the
screen, but there are some differences between them:
 echo doesn't have a return value and can take multiple
parameters.
 print always has a return value of 1 and can only take
one parameter.
 echo is generally considered to be slightly faster than
print because it doesn't return a value.

3)Which is the use of isset()function?


Ans: The isset () function is an inbuilt function in PHP which is
used to determine if the variable is declared and its value is not
equal to NULL.
Syntax:bool isset ( mixed $var [, mixed $... ] )

4)which are a method to submit a form?


Ans: In PHP, submitting a form typically involves using the HTTP
POST or GET methods. Forms are HTML elements that allow users
to input data and submit it to the server for processing.
.HTML, forms are defined using the <form> element.
The method attribute of the form specifies how the data will
be sent to the server. Use method="post" to send data via
the POST method.
When the form is submitted, the data is sent to the server in
the HTTP request body, making it more secure than GET for
sensitive information.
Use method="get" in the form tag to submit data via the GET
method.
Data is appended to the URL, making it visible in the address bar.
It is less secure than POST, so avoid using it for sensitive
information.

5)explain setcookie () in php?


Ans: in PHP, the setcookie() function is used to set a cookie for
the client's browser. Cookies are small pieces of data that are
stored on the user's computer and are sent with each request to a
specific domain. They are often used to store information about
the user or to track user activity on a website.

6)what is $-SESSION in php?


Ans: In PHP, $_SESSION is a super global variable that is used to
store session variables across multiple pages. Session variables
are used to store and retrieve user-specific information as the
user navigates through different pages of a website.

7)Explain split () function in php?


Ans: he split () function was used to split a string into an array of
substrings based on a specified pattern. Its basic syntax was:
split (pattern, string, limit);

8)What does PEAR stands for?


Ans: PEAR stands for "PHP Extension and Application Repository."
PEAR is a framework and distribution system for reusable PHP
components and packages. It provides a structured library of
open-source code for PHP users, making it easier to manage and
share code among developers.
9)What is the use of print_r ()?
Ans: The print_r () function in PHP is used for debugging and
displaying human-readable information about a variable. It stands
for "print recursive" and is particularly useful for examining the
contents of arrays, objects, or other complex data structures.

10)What does the unset () function mean?


Ans: The unset () function in PHP is used to destroy a given
variable. It can be used to remove a variable, an element from an
array, or elements from an array.
SYNTAX: unset (var1, var2, ...);

1)what are the given type of php variables?


Ans: Variables in a program are used to store some values or data
that can be used later in a program. The variables are also like
containers that store character values, numeric values, memory
addresses, and strings. PHP has its own way of declaring and
storing variables.
Local variables: The variables declared within a function are
called local variables to that function and have their scope only in
that particular function. In simple words, it cannot be accessed
outside that function. Any declaration of a variable outside the
function with the same name as that of the one within the
function is a completely different variable. We will learn about
functions in detail in later articles. For now, consider a function as
a block of statements.
Global variables: The variables declared outside a function are
called global variables. These variables can be accessed directly
outside a function. To get access within a function we need to use
the “global” keyword before the variable to refer to the global
variable.
Static variable: It is the characteristic of PHP to delete the
variable, once it completes its execution and the memory is freed.
But sometimes we need to store the variables even after the
completion of function execution. To do this we use the static
keywords and the variables are then called static variables. PHP
associates a data type depending on the value for the variable.

2)What is difference between PHP constant and Variable?


Ans: difference between PHP Constants and PHP Variables
PHP Constants PHP Variables

In PHP constants there is no need to use $


In PHP Variables the $ sign is been used.
sign.

The data type of PHP constant cannot be The data type of the PHP variable can be
changed during the changed during the
execution of the script. execution of the script.

A PHP constant once defined cannot be A PHP variable can be undefined as well
redefined. as can be redefined.

We can not define a constant using any


simple assignment operator We can define a variable using a simple
assignment operation(=).
rather it can only be defined using define().

On the other hand, variables are written in


Usually, constants are written in numbers.
letters and symbols.

PHP constants are automatically global PHP variables are not automatically global
across the entire script. in the entire script.

PHP constant is comparatively slower than A PHP variable is comparatively faster


PHP variable than the PHP constant

3)explain the syntax for each loop with example?


Ans: The foreach loop works only on arrays, and is used to loop
through each key/value pair in an array.
Syntax
foreach ($array as $value) {
code to be executed;
}
For every loop iteration, the value of the current array element is
assigned to $value and the array pointer is moved by one, until it
reaches the last array element.
Examples
The following example will output the values of the given array
($colors):
<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {


echo "$value <br>";
}
?>

4)what are the different type of array in php?


Ans: Arrays in PHP is a type of data structure that allows us to store
multiple elements of similar data type under a single variable thereby
saving us the effort of creating a different variable for every data.
There are basically three types of arrays in PHP:
Indexed Arrays:
 An indexed array is a simple list of elements where each
element is assigned a numeric index.
 Indexes start from 0 and increase sequentially.
 Example: $colors = array("red", "green", "blue");
Associative Arrays:
• In an associative array, each element is associated with a
specific key instead of a numeric index.
• Keys can be strings or integers.
• Example: person = array("name" => "John", "age" => 30,
"city" => "New York");

Multidimensional Arrays:
 A multidimensional array is an array of arrays, or an
array whose elements can be arrays.
 Useful for representing tables or matrices.
 Example:$matrix = array(
 array(1, 2, 3),
 array(4, 5, 6),
 array(7, 8, 9)
 );

5)what is session in php? Explain it.


Ans: When you work with an application, you open it, do some
changes, and then you close it. This is much like a Session.
The computer knows who you are. It knows when you start
the application and when you end. But on the internet there is
one problem: the web server does not know who you are or
what you do, because the HTTP address doesn't maintain
state.
Session variables solve this problem by storing user
information to be used across multiple pages (e.g. username,
favorite color, etc). By default, session variables last until the
user closes the browser.
session is started with the session_start() function.
Session variables are set with the PHP global variable:
$_SESSION.
Now, let's create a new page called "demo_session1.php".
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>

6)Difference Between Client-side scripting and server side


scripting?
Ans:

features Server-side Scripting Client-side Scripting

Primary The main function of this The main purpose of


Function scripting is to manipulate this scripting is to give
and grant access to the the requested output to
requested database. the end-user.

Uses It is employed at the It is utilized at the front


backend, where the source end, which users may
code is invisible on the client view through the
side. browser.

Processing It needs server interaction. It doesn't need any


server interaction.

Security It is more secure while It is less secure than


working on a web app. server-side scripting
due to the code
accessibility offered to
the client.

Running It executes on the web It executes on the


server. remote computer
system.

Dependabili It doesn't depend on the It depends on the user's


ty client. browser version.

File Access It offers complete access to It doesn't offer any


the file that is stored in the access to the files on
web database server. the web servers.

Languages The server-side scripting Its programming


Involved programming languages, languages are HTML,
such as PHP, ColdFusion, CSS, and JavaScript.
Python, ASP.net, Java, C++,
Ruby, C#, etc.

7)write a function with default parameter in php using


example.
Ans: <?php
function greet ($name = "Guest") {
echo "Hello, $name!";
}

// Calling the function without providing a value for $name


greet (); // Output: Hello, Guest

// Calling the function with a specific value for $name


greet("John"); // Output: Hello, John
?>
In this example, the greet function has a parameter $name
with a default value of "Guest." When the function is called
without providing a value for $name, it uses the default value.
If a specific value is provided, it uses that value instead.

8)write a php script to accept user’s name and display in n


next page.
Ans: <!DOCTYPE html>
<html>

<body>
<!-- Heading -->
<h3> HTML input form </h3>

<!-- HTML form -->


<form method="POST">
<h4>Please enter your First Name : </h4>
<input type="text" name="f_name"><br>
<h4>Please enter your Last Name : </h4>
<input type="text" name="l_name"><br><br>

<input type="submit" value="Display" name="submit">


</form>
</body>

</html>
<?php

// When the submit button is clicked


if (isset($_POST['submit'])) {

// Creating variables and


// storing values in it
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];

echo "<h1><i> Good Morning, $f_name $l_name


</i></h1>";
}
?>

9)write a php program to display following operation on


string:
1)string concatenation
2)string comparison
Ans:

You might also like