0% found this document useful (0 votes)
26 views7 pages

Finals Notes

PHP has several built-in functions that can be used to perform common tasks without needing additional libraries. Some key built-in functions include strlen() for getting the length of a string, str_replace() for replacing substrings, array_push() for adding elements to an array, and date() for formatting dates and times. Built-in functions provide convenient ways to manipulate strings, arrays, files, dates and more within PHP applications.

Uploaded by

sadaf abid
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)
26 views7 pages

Finals Notes

PHP has several built-in functions that can be used to perform common tasks without needing additional libraries. Some key built-in functions include strlen() for getting the length of a string, str_replace() for replacing substrings, array_push() for adding elements to an array, and date() for formatting dates and times. Built-in functions provide convenient ways to manipulate strings, arrays, files, dates and more within PHP applications.

Uploaded by

sadaf abid
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/ 7

Index & Associative Array in php

Associative Array:

:- In PHP, an associative array is a collection of key-value pairs, where each key is unique and associated
with a specific value.

To create an associative array, you can use the array() function with key-value pairs as arguments, like
this:
Key Value

Example: $person = array("name" => "John", "age" => 30, "city" => "New York");

In this example, $person is an associative array.

You can access the values of an associative array by using the key, like this:

echo $person["name"]; // Outputs: John

Index Array:

An index array, is a collection of values that are accessed by their numeric index. You can create an index
array by using the array() function with values as arguments, like this:

Example: $numbers = array(1, 2, 3, 4, 5);

In this example, $numbers is an index array with five values: 1, 2, 3, 4, and 5.

You can access the values of an index array by using the index, like this:

echo $numbers[0]; // Outputs: 1

Difference between GET vs POST

Difference GET Method POST Method

Data Visibility Data is visible in the URL Data is not visible in the URL

Large amount of data can be


Data Amount Limited amount of data can be sent
sent

Caching Can be cached Cannot be cached

More secure because data is


Security Less secure because data is visible in URL
not visible in URL

Data Type Can only send ASCII characters Can send binary data

Typically used for submitting


Usage Typically used for retrieving data
data to the server
HTTP & HTTPS

Difference HTTP HTTPS


Security HTTP is less secure than HTTPS, as it does not HTTPS is more secure than HTTP, as it encrypts
encrypt data transmitted between the client and data using SSL/TLS encryption.
the server.

Port HTTP uses Port 80 by default HTTPS uses Port 443 by default

Encryption HTTP does not use encryption HTTPS uses SSL/TLS encryption to protect the data
transmitted over the internet.
URL URLs that begin with "http://" are unencrypted URLs that begin with "https://" are encrypted, and
and can be intercepted by third parties. any data transmitted between the client and
server is protected.

Default HTTP is the default protocol for most websites, as HTTPS is used for websites that require additional
it is faster and does not require the installation of security measures, such as online banking, e-
an SSL certificate. commerce, and social media sites that require user
login information.

Speed HTTP is slightly faster than HTTPS HTTPS is slightly slower than HTTP due to the
additional overhead of SSL/TLS encryption.

SQL Query
SQL (Structured Query Language) is a programming language used to manage and manipulate relational
databases. An SQL query is a statement that is written in SQL syntax and is used to retrieve, insert,
update, or delete data from a database.

An SQL query can be used to perform a wide range of database operations, such as:

• SELECT: retrieve data from one or more tables


• INSERT: insert data into a table
• UPDATE: modify existing data in a table
• DELETE: delete data from a table
• CREATE: create a new table, index, or other database object

An SQL query consists of a series of clauses that specify condition. These clauses include keywords like
SELECT, FROM, WHERE, GROUP BY, ORDER BY, and others.
Here's an example of a basic SQL query that retrieves all records from a table named "customers":

SELECT * FROM customers;

For & for each Loop


For Loop: The for loop is used when you need to execute a block of code a specific number of times. It
consists of three parts:

1. Initialization: This part initializes a counter variable before the loop starts.
2. Condition: This part checks the condition before each iteration of the loop. If the condition is
true, the loop continues to execute. If it's false, the loop terminates.
3. Increment: This part updates the counter variable after each iteration of the loop.

Here's an example of a for loop that prints the numbers from 1 to 10:

for ($i = 1; $i <= 10; $i++) {

echo $i . "<br>";

Foreach Loop: The foreach loop is used when you need to iterate over the elements of an array or an
object.

It automatically assigns the value of each element to a variable on each iteration of the loop.

Here's an example of a foreach loop that prints the values of an array:

$numbers = array(1, 2, 3, 4, 5);

foreach ($numbers as $number) {

echo $number . "<br>";

Major Difference

One key difference between the for loop and the foreach loop is that the for loop requires you to
manually initialize and update the counter variable, while the foreach loop automatically iterates over
the elements of an array or object. This makes the foreach loop more convenient to use when working
with arrays and objects.

Super Global Variable


Super Global variables in PHP are a special type of variables that are predefined by PHP and can be
accessed from anywhere in a PHP script, without the need to declare them first. These variables are
called "Super Global" because they are available in all scopes of a PHP script, including functions, classes,
and global code.
PHP has several super global variables, including:

1. $_SERVER: This variable contains information about the web server and the current request
2. $_GET: This variable contains data that was sent to the PHP script through the HTTP GET
method.
3. $_POST: This variable contains data that was sent to the PHP script through the HTTP POST
method.
4. $_REQUEST: This variable contains data that was sent to the PHP script through either the GET
or POST methods.
5. $_SESSION: This variable contains data that is stored across multiple requests by the same
client.
6. $_COOKIE: This variable contains data that was sent to the PHP script in an HTTP cookie.
7. $_FILES: This variable contains information about files that were uploaded to the PHP script
using the HTTP POST method.

Cookies
Cookies: are small pieces of data that are stored on the client's computer and can be used to store
information such as login credentials or user preferences.

To set a cookie in PHP, you can use the setcookie() function. This function takes three parameters:

name: The name of the cookie.


value: The value of the cookie.
expire: The expiration time of the cookie, in seconds since the Unix epoch. If you set this to 0,
the cookie will expire when the browser is closed.

Here's an example of setting a cookie in PHP:


setcookie('username', 'johndoe', time() + 3600);

To retrieve a cookie in PHP, you can use the $_COOKIE superglobal variable.

Php Built in Functions


In PHP, built-in functions are pre-defined functions that can be used to perform various tasks.

These functions are available in PHP by default and can be used without the need for any additional
libraries or modules.

Some examples of built-in functions in PHP include:

1. strlen(): This function returns the length of a string.


2. str_replace(): This function replaces all occurrences of a string in another string.
3. array_push(): This function adds one or more elements to the end of an array.
4. date(): This function returns the current date and time in a specified format.
5. rand(): This function generates a random number.
6. strtolower(): This function converts a string to lowercase.
7. substr(): This function returns a substring of a string.
Here are some more examples of built-in functions in PHP:

• count(): This function returns the number of elements in an array.


• explode(): This function splits a string into an array based on a delimiter.
• implode(): This function joins the elements of an array into a string, using a specified delimiter.
• array_sum(): This function returns the sum of all the elements in an array.
• is_numeric(): This function checks if a value is numeric.
• file_get_contents(): This function reads the contents of a file into a string.
• header(): This function sends an HTTP header to the client.

Array Sorting Functions in Php


These functions allow you to sort arrays based on various criteria, such as the value of each.

Here are some of the most commonly used array sorting functions in PHP:

1. sort(): This function sorts an array in ascending order based on the value of each element.
2. rsort(): This function sorts an array in descending order based on the value of each element.
3. asort(): This function sorts an array in ascending order based on the value of each element,
while maintaining the key-value associations.
4. arsort(): This function sorts an array in descending order based on the value of each element,
while maintaining the key-value associations.
5. ksort(): This function sorts an array in ascending order based on the key associated with each
element.
6. krsort(): This function sorts an array in descending order based on the key associated with each
element.
7. usort(): This function allows you to define your own comparison function to sort the array.
8. uasort(): This function allows you to define your own comparison function to sort the array,
while maintaining the key-value associations.
9. uksort(): This function allows you to define your own comparison function to sort the array
based on the key associated with each element.
Php code for inserting, deleting & updating.
Insertion Data:

// establish database connection

$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "myDB";

$conn = mysqli_connect($servername, $username, $password, $dbname);

// insert data into table

$sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe',
'[email protected]')";

if (mysqli_query($conn, $sql)) {

echo "New record created successfully";

} else {

echo "Error: " . $sql . "<br>" . mysqli_error($conn);

// close database connection

mysqli_close($conn);

Deleting Data:

// establish database connection

$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "myDB";

$conn = mysqli_connect($servername, $username, $password, $dbname);


// delete data from table

$sql = "DELETE FROM MyGuests WHERE id=3";

if (mysqli_query($conn, $sql)) {

echo "Record deleted successfully";

} else {

echo "Error deleting record: " . mysqli_error($conn);

// close database connection

mysqli_close($conn);

Updating Data:

// establish database connection

$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "myDB";

$conn = mysqli_connect($servername, $username, $password, $dbname);

// update data in table

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if (mysqli_query($conn, $sql)) {

echo "Record updated successfully";

} else {

echo "Error updating record: " . mysqli_error($conn);

// close database connection

mysqli_close($conn);

You might also like