Sunteți pe pagina 1din 13

PHP

What is PHP?

 PHP: Hypertext PreProcessor


o Stood for other things before, began 1995
 Server-Side Programming/Scripting Language
o All php is hosted on a server and is not like JS which can be ran on user’s client
machine, it needs a server to run.
o Apache is the most popular server that php runs on can use others
o Apache is apart of XAMPP stack which is Linux, apache MySQL and php and that is
extremely popular stack to work with.
 Can be embedded directly with HTML (<?php?>)
 File use a “.php” file extension

How does PHP work?

 Client makes a request


o Starts with user on a client machine making a page request usually to php file.
 Server runs PHP/extensions
o Then server processes that request and uses a PHP interpreter
o PHP has a bunch of extensions, so are enabled by default and you can add and
remove extensions. Can even create your own.
 Query database if needed
o While PHP is being processed it has the ability to work with database,
o That’s one of PHP best features that you can use it with almost any type of database.
o MySQL is most popular one for small and medium sized application.
 Load other files if needed
o Can also interact with other files on the system, can read from other files aswell as
write to them.
 Result is sent back to client
o Once the processing and interpreting is complete it will send the result to the client
or the browser.
o Important to understand the actual PHP script and PHP programming is not sent to
browser, it doesn’t work like it does if you were just requesting a HTML page. It
rather sends the result of the script and this is usually HTML. But PHP can also send
plain text, JSON, XML, pdf’s etc. Usually just sends the HTML result. If you were to
view your source code in your browser you will see HTML, you will see the client side
JS, you will see the CSS but you won’t see the PHP.

Why Use PHP?

 Easy to learn
o Not typed, not have to define data types etc.
 Free & open source
 Great support
o Good documentation.
 Cross platform
o Works on windows, Linux, mac and runs on APACHE server can use others
 Freedom
 Frameworks
 Database Compatibility
o Compatible with almost all types of databases.

What can PHP do?

 Create dynamic page content


 Interact with files on the server
 Collect & process forms
 Send/receive cookies to and from the browser
 Interact with databases, so create and delete data
 Access control
 Encrypt data
 Almost anything else

What can you build with PHP?

 Basic websites
 Blog type websites
 Shopping carts
 Social networks
 Content management systems
 Membership websites with access control
 Search engines
 Backend APIs

When working with server, the folder you want to work in is c:\xmapp\htdocs

This is basically the root of your web server, it is loading this index.php file just like when you work
with HTML, you name it index.html to be the root, same with php you name it index.php and it will
load automatically.
Access local host via https://localhost

JSON
JSON stands for JavaScript Object Notation. JSON objects are used for transferring data between
server and client, XML serves the same purpose. However, JSON objects have several advantages
over XML and we are going to discuss them in this tutorial along with JSON concepts and its usages.

Let’s have a look at the piece of a JSON data: It basically has key-value pairs, need double quotes.

Features of JSON:

 It is light-weight
 It is language independent
 Easy to read and write
 Text based, human readable data exchange format

Why use JSON?

Standard Structure: As we have seen so far that JSON objects are having a standard structure that
makes developers job easy to read and write code, because they know what to expect from JSON.

Light weight: When working with AJAX, it is important to load the data quickly and asynchronously
without requesting the page re-load. Since JSON is light weighted, it becomes easier to get and load
the requested data quickly.

Scalable: JSON is language independent, which means it can work well with most of the modern
programming language. Let’s say if we need to change the server-side language, in that case it would
be easier for us to go ahead with that change as JSON structure is same for all the languages.

JSON vs. XML

Let see how JSON and XML look when we store the records of 4 students in a text-based format so
that we can retrieve it later when required.

JSON style:
XML style:

As you can clearly see JSON is much more light-weight compared to XML. Also, in JSON we take
advantage of arrays that is not available in XML.

JSON Syntax Rules

JSON syntax is derived from JavaScript object notation syntax:

 Data is in name/value pairs


o name/ value pairs need double quotes
 Data is separated by commas
 Curly braces hold objects
 Square brackets hold arrays

Valid Data Types

In JSON, values must be one of the following data types:

 a string
 a number
 an object (JSON object)
 an array
 a boolean
 null

JSON values cannot be one of the following data types:

 a function
 a date
 undefined
Strings

Strings in JSON must be written in double quotes.

Example:

 { "name":"John" }

Numbers

Numbers in JSON must be an integer or a floating point.

Example:

 { "age":30 }

Objects

Values in JSON can be objects.

Accessing Object Values

You can access the object values by using dot (.) notation:

You can also access the object values by using bracket ([]) notation:

Nesting Objects

Values in a JSON object can be another JSON object


You can access nested JSON objects by using the dot notation or bracket notation:

Modify Values

You can use the dot notation to modify any value in a JSON object:

You can also use the bracket notation to modify a value in a JSON object:

Delete Object Properties

Use the delete keyword to delete properties from a JSON object:

Arrays

Values in JSON can be arrays.

Booleans

Example:

 { "sale":true }

Null

Example:

 { "middlename":null }
JSON.parse()

A common use of JSON is to exchange data to/from a web server.

When receiving data from a web server, the data is always a string.

Parse the data with JSON.parse(), and the data becomes a JavaScript object.

Example - Parsing JSON

Imagine we received this text from a web server:

Use the JavaScript function JSON.parse() to convert text into a JavaScript object:

Note: Make sure the text is written in JSON format, or else you will get a syntax error.

Use the JavaScript object in your page:

JSON From the Server

You can request JSON from the server by using an AJAX request

As long as the response from the server is written in JSON format, you can parse the string into a
JavaScript object.

Example: Use the XMLHttpRequest to get data from the server:


Array as JSON

When using the JSON.parse() on a JSON derived from an array, the method will return a
JavaScript array, instead of a JavaScript object.

Example: The JSON returned from the server is an array:


Exceptions

Parsing Dates

Date objects are not allowed in JSON.

If you need to include a date, write it as a string.

You can convert it back into a date object later:

Example: Convert a string into a date:

Or, you can use the second parameter, of the JSON.parse() function, called reviver.

The reviver parameter is a function that checks each property, before returning the value.

Example: Convert a string into a date, using the reviver function:


Parsing Functions

Functions are not allowed in JSON.

If you need to include a function, write it as a string.

You can convert it back into a function later:

Example: Convert a string into a function:

You should avoid using functions in JSON, the functions will lose their scope, and you would have to
use eval() to convert them back into functions.

JSON.stringify()

A common use of JSON is to exchange data to/from a web server.

When sending data to a web server, the data has to be a string.

Convert a JavaScript object into a string with JSON.stringify().

Stringify a JavaScript Object

Imagine we have this object in JavaScript:

Use the JavaScript function JSON.stringify() to convert it into a string.

The result will be a string following the JSON notation.

myJSON is now a string, and ready to be sent to a server:


Example

You will learn how to send JSON to the server in the next chapter.

Stringify a JavaScript Array

It is also possible to stringify JavaScript arrays:

Imagine we have this array in JavaScript:

Use the JavaScript function JSON.stringify() to convert it into a string.

The result will be a string following the JSON notation.

myJSON is now a string, and ready to be sent to a server:

Example
Exceptions

Stringify Dates

In JSON, date objects are not allowed. The JSON.stringify() function will convert any dates
into strings.

You can convert the string back into a date object at the receiver.

Stringify Functions

In JSON, functions are not allowed as object values.

The JSON.stringify() function will remove any functions from a JavaScript object, both the
key and the value:
This can be omitted if you convert your functions into strings before running the
JSON.stringify() function.

You should avoid using functions in JSON, the functions will lose their scope, and you would have to
use eval() to convert them back into functions.

S-ar putea să vă placă și