Documente Academic
Documente Profesional
Documente Cultură
What is 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.
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
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.
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.
a string
a number
an object (JSON object)
an array
a boolean
null
a function
a date
undefined
Strings
Example:
{ "name":"John" }
Numbers
Example:
{ "age":30 }
Objects
You can access the object values by using dot (.) notation:
You can also access the object values by using bracket ([]) notation:
Nesting Objects
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:
Arrays
Booleans
Example:
{ "sale":true }
Null
Example:
{ "middlename":null }
JSON.parse()
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.
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.
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.
When using the JSON.parse() on a JSON derived from an array, the method will return a
JavaScript array, instead of a JavaScript object.
Parsing Dates
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.
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()
You will learn how to send JSON to the server in the next chapter.
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
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.