CSS Syntax

A CSS rule set contains a selector and a declaration block.

CSS syntax

Selector: Selector indicates the HTML element you want to style. It could be any tag like <h1>, <title> etc.

Declaration Block: The declaration block can contain one or more declarations separated by a semicolon. For the above example, there are two declarations:

  1. color: yellow;
  2. font-size: 11 px;

Each declaration contains a property name and value, separated by a colon.

Property: A Property is a type of attribute of HTML element. It could be color, border etc.

Value: Values are assigned to CSS properties. In the above example, value “yellow” is assigned to color property.

  1. Selector{Property1: value1; Property2: value2; ……….;}  

PHP Mail

PHP mail() function is used to send email in PHP. You can send text message, html message and attachment with message using PHP mail() function.

PHP mail() function

Syntax

  1. bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )  

$to: specifies receiver or receivers of the mail. The receiver must be specified one of the following forms.

  • user@example.com
  • user@example.com, anotheruser@example.com
  • User <user@example.com>
  • User <user@example.com>, Another User <anotheruser@example.com>

$subject: represents subject of the mail.

$message: represents message of the mail to be sent.

Note: Each line of the message should be separated with a CRLF ( \r\n ) and lines should not be larger than 70 characters.

$additional_headers (optional): specifies the additional headers such as From, CC, BCC etc. Extra additional headers should also be separated with CRLF ( \r\n ).


PHP Mail Example

<?php  
   ini_set("sendmail_from", "sonoojaiswal@javatpoint.com");  
   $to = "sonoojaiswal1987@gmail.com";//change receiver address  
   $subject = "This is subject";  
   $message = "This is simple text message.";  
   $header = "From:sonoojaiswal@javatpoint.com \r\n";  
  
   $result = mail ($to,$subject,$message,$header);  
  
   if( $result == true ){  
      echo "Message sent successfully...";  
   }else{  
      echo "Sorry, unable to send mail...";  
   }  
?>  

If you run this code on the live server, it will send an email to the specified receiver.


PHP Mail: Send HTML Message

To send HTML message, you need to mention Content-type text/html in the message header.

<?php  
   $to = "abc@example.com";//change receiver address  
   $subject = "This is subject";  
   $message = "<h1>This is HTML heading</h1>";  
  
   $header = "From:xyz@example.com \r\n";  
   $header .= "MIME-Version: 1.0 \r\n";  
   $header .= "Content-type: text/html;charset=UTF-8 \r\n";  
  
   $result = mail ($to,$subject,$message,$header);  
  
   if( $result == true ){  
      echo "Message sent successfully...";  
   }else{  
      echo "Sorry, unable to send mail...";  
   }  
?>  

PHP Mail: Send Mail with Attachment

To send message with attachment, you need to mention many header information which is used in the example given below.

<?php  
  $to = "abc@example.com";  
  $subject = "This is subject";  
  $message = "This is a text message.";  
  # Open a file  
  $file = fopen("/tmp/test.txt", "r" );//change your file location  
  if( $file == false )  
  {  
     echo "Error in opening file";  
     exit();  
  }  
  # Read the file into a variable  
  $size = filesize("/tmp/test.txt");  
  $content = fread( $file, $size);  
  
  # encode the data for safe transit  
  # and insert \r\n after every 76 chars.  
  $encoded_content = chunk_split( base64_encode($content));  
    
  # Get a random 32 bit number using time() as seed.  
  $num = md5( time() );  
  
  # Define the main headers.  
  $header = "From:xyz@example.com\r\n";  
  $header .= "MIME-Version: 1.0\r\n";  
  $header .= "Content-Type: multipart/mixed; ";  
  $header .= "boundary=$num\r\n";  
  $header .= "--$num\r\n";  
  
  # Define the message section  
  $header .= "Content-Type: text/plain\r\n";  
  $header .= "Content-Transfer-Encoding:8bit\r\n\n";  
  $header .= "$message\r\n";  
  $header .= "--$num\r\n";  
  
  # Define the attachment section  
  $header .= "Content-Type:  multipart/mixed; ";  
  $header .= "name=\"test.txt\"\r\n";  
  $header .= "Content-Transfer-Encoding:base64\r\n";  
  $header .= "Content-Disposition:attachment; ";  
  $header .= "filename=\"test.txt\"\r\n\n";  
  $header .= "$encoded_content\r\n";  
  $header .= "--$num--";  
  
  # Send email now  
  $result = mail ( $to, $subject, "", $header );  
  if( $result == true ){  
      echo "Message sent successfully...";  
   }else{  
      echo "Sorry, unable to send mail...";  
   }  
?>  

How to create newline in PHP?

To create a newline, PHP provides nl2br() function. It is an in-built function of PHP, which is used to insert the HTML line breaks before all newlines in the string. Although, we can also use PHP newline character \n or \r\n inside the source code to create the newline, but these line breaks will not be visible on the browser. So, the nl2br() is useful here.

The nl2br() function contains these newline characters \n or \r\n which combinely create the newline. Apart from nl2br() function, a html break line tag </br> is used to break the string. The </br> tag should be enclosed in double quotes, e.g., “</br>”.

For Example

To create the newline in PHP, let’s see the number of examples.

Example 1

Let’s take an of example to create the newline with the help of nl2br() function.

  1. <?php  
  2.     echo nl2br(“New line will start from here\n in this string\r\n on the browser window”);  
  3. ?>  

Output

In this example, we can see that the nl2br() function has contained “\n” and “\r\n” characters in the string for newline. We can see the result of the following program in the below output.

New line will start from here
in this string
on the browser window

Example 2

<?php  
    echo "One line after\n another line";  
    echo " One line after\r\n another line";  
?>  

Output

In the above example, we can see that after “\n” or “\r\n” character, string did not start from the new line. “\n” and “\r\n” alone are not enough for creating a newline in the string, as the whole string is displayed in a single line.

One line after another line One line after another line

Note: The character “/n” is used in Linux to write a newline whereas in windows “\r\n” is used. For the safe side use the “\r\n” character for creating newline instead.

Example 3

<?php  
    echo "One line after\n another line";  
    echo "</br>";  
    echo "One line after\r\n another line";  
?>  

Output

Here, we break the lines using html break line tag “</br>“.

One line after another line
One line after another line

PHP vs Node.js

What is PHP?

PHP stands for Hypertext Preprocessor, which is an open source scripting language. It is a server-side scripting language and a powerful tool for creating a dynamic and interactive website.

PHP is an interpreted language, so it doesn’t need compilation. It is specially designed for server-side scripting, which executes on the server. PHP can be easily embedded with HTML files.

Note: PHP is mainly used to develop server-side applications.

It has several advantages that are given below:

  • We can execute PHP code on different platform such as Windows, Linux, UNIX, Solaris, etc.
  • It is easy to use and learn.
  • PHP provides a built-in module which helps it to connect with the database easily.
  • PHP is an open source language that means it is available for free of cost.

In general, PHP is cheap, cross-platform, fast, and reliable to develop web applications.


What is Node.js?

Node.js is a JavaScript programming language which runs on the server. It helps to create dynamic and interactive web pages. Node.js file is saved with .js extension, and it only contains JavaScript code. It executes JavaScript code outside the browser.

Node.js is an open-source language which executes in different environments such as Windows, Linux, UNIX, and Mac OS, etc.

Node.js has many advantages which are listed below.

  • Node.js is fast and lightweight.
  • It is more secure than PHP.
  • Node.js allows us to write JavaScript code for both client and server-side.
  • Node.js offers scalability, i.e., it easy to scale the application vertically as well as horizontally.
  • JavaScript is now available for every browser and as well as it can run on each server due to Node.js.

Difference between PHP and Node.js

PHP and Node.js both are server-side scripting languages; thus, they have become the competitor for each other. They are bound to have some similarities and also some differences. Following are some differences based on their functionality and features.

FeaturesPHPNode.js
Runtime EnvironmentPHP is straightforward to install and use at server-side.PHP is straightforward to install and use at server-side.
Powered byPHP is powered by Zend engine.Node.js is powered by Google’s v8 JavaScript engine.
ExecutionPHP is synchronous except some APIs.It is totally asynchronous.
FrameworkPHP has many frameworks for easy backend development, such as Laravel, CakePHP, etc.Node.js also has popular frameworks like Express, Meteor, and DerbyJS, etc.
Execution SpeedPHP execution speed is slower than Node.js.Node.js is faster than PHP and lightweight too.
Web ServerPHP needs Apache web server to execute the code.Node.js doesn’t need any web server to execute. It runs in its own environment.
Compatibility with other languagesPHP can contain HTML, JavaScript, CSS, and even plain text.Node.js can contain only JavaScript.
Used byFacebook, Wikipedia, Yahoo, Flickr, and WordPress, etc., are using PHP.IBM, GoDaddy, NetFlix, LinkedIn, Paypal, and Walmart are the adopters of Ndoe.js.
ComplexityPHP is simpler to use than Node.js.Node.js is not too complex, but need more lines of code and callback functions.
Basic syntaxecho ‘Hello PHP’;Console.log(‘Hello Node.js’);
ModuleA developer needs to download and install PHP manually. It doesn’t come in bundled with module.It comes prepackaged with the NPM package management system and its registry.
PerformancePHP is fast, but slower than Node.js due to the database, third-party request, and file system.Node.js is faster due to its non-blocking mechanism.

PHP vs HTML

What is PHP?

PHP stands for Hypertext Preprocessor, which is an open source scripting language. It is a server-side scripting language and a powerful tool for creating a dynamic and interactive website.

PHP is an interpreted language, so it doesn’t need compilation. It is specially designed for server-side scripting, which executes on the server. PHP can be easily embedded with HTML files.

Note: PHP is mainly used to develop server-side applications.

It has several advantages that are given below:

  • We can execute PHP code on different platform such as Windows, Linux, UNIX, Solaris, etc.
  • It is easy to use and learn.
  • PHP provides a built-in module which helps it to connect with the database easily.
  • PHP is an open source language that means it is available for free of cost.

In general, PHP is cheap, cross-platform, fast, and reliable to develop web applications.

What is HTML?

HTML stands for Hypertext Markup Language, which is used to create web pages. It is basically used to create static web pages, but it can integrate with CSS, JavaScript, and PHP.

HTML is not a programming language, as it is a tag-based language. Angular brackets <> are used to represents HTML elements or tags.

  • HTML is very easy to learn and implement.
  • It is not a case-sensitive language.
  • We can write HTML code on any text editor like Notepad, Notepad++, Edit plus, etc.
  • HTML is platform-independent, hence, it can be executed on different platform.
  • It allows the programmer to add colors, audio, video, and images on a web page.

Difference between PHP and HTML

PHPHTML
PHP is a server-side programming language.HTML is a client-side scripting language.
PHP is used in backend development, which interacts with databases to retrieve, store, and modify the information.HTML is used in frontend development, which organizes the content of the website.
PHP is used to create a dynamic website. The output will depend on the browser.HTML is used to create a static website. The output of static website remains the same on each time.
PHP can manipulate the data.It cannot manipulate the data.
PHP code executes on web servers like Apache web server, IIS web server.HTML code executes on web browsers like Chrome, Internet Explorer, etc.
PHP is scripting language.HTML is a markup language.
PHP7.3 is the latest version of PHP.HTML5.2 is the latest version of HTML.
PHP is also easy to learn but not as simple as HTML.HTML is easy to learn. It can easily learn in a very short time.
PHP files save with .php extension.HTML files save with .html extension.

MVC Architecture

MVC is a software architectural pattern for implementing user interfaces on computers. It divides a given application into three interconnected parts. This is done to separate internal representations of information from the ways information is presented to, and accepted from the user.

  • MVC stands for “Model view And Controller“.
  • The main aim of MVC Architecture is to separate the Business logic & Application data from the USER interface.
  • Different types of Architectures are available. These are 3-tier Architecture, N-tier Architecture, MVC Architecture, etc.
  • The main advantage of Architecture is Reusability, Security and Increasing the performance of Application.
PHP MVC Architecture

Model: Database operation such as fetch data or update data etc.

View: End-user GUI through which user can interact with system, i.e., HTML, CSS

Controller: Contain Business logic and provide a link between model and view.

Let’s understand this MVC concept in detail:

Model:

  • The Model object knows all about all the data that need to be displayed.
  • The Model represents the application data and business rules that govern to an update of data.
  • Model is not aware about the presentation of data and How the data will be display to the browser.

View:

  • The View represents the presentation of the application.
  • View object refers to the model remains same if there are any modifications in the Business logic.
  • In other words, we can say that it is the responsibility of view to maintain consistency in its presentation and the model changes.

Controller:

  • Whenever the user sends a request for something, it always goes through Controller.
  • A controller is responsible for intercepting the request from view and passes to the model for appropriate action.
  • After the action has been taken on the data, the controller is responsible for directly passes the appropriate view to the user.
  • In graphical user interfaces, controller and view work very closely together.

Inheritance Task

Create program to find area of Triangle using inheritance.

Solution:

<?php  
    class getdata  
    {  
        public $h;  
        public $b;  
        public function method1()  
        {  
            $this->h= $_REQUEST['height'];  
            $this->b= $_REQUEST['base'];  
        }  
  
    }  
    classfindarea extends getdata  
    {  
        public $area;  
        public function method2()  
        {  
            $this->area= ($this->h* $this->b)/2;  
            echo "Area of Triangle is : ".$this->area;  
        }  
    }  
    if(isset($_REQUEST['submit']))  
    {  
        $obj = new findarea();  
        $obj->method1();  
        $obj->method2();  
    }  
  
      
?>  
<html>  
<head>  
</head>  
<body>  
    <form method= "post">  
        <table align ="left" border="1">  
        <tr>  
            <td> Enter Height </td>  
            <td><input type = "text" name="height"/></td>  
        </tr>  
        <tr>  
            <td> Enter Base </td>  
            <td><input type = "text" name="base"/></td>  
        </tr>  
        <tr>  
            <td align ="center" colspan="2">  
            <input type= "submit"name="submit"value="click"/>  
        </td>  
        </tr>  
        </table>  
        </form>  
</body>  
</html>  

Output:

Inheritance Task
Inheritance Task
Inheritance Task

Special Types

There are 2 special data types in PHP

  1. Resource
  2. Null

Resource Data type:

It refers the external resources like database connection, FTP connection, file pointers, etc. In simple terms, a resource is a special variable which carrying a reference to an external resource.

Example 1

<?php  
    $conn = ftp_connect("127.0.0.1") or die("Could not connect");  
    echo get_resource_type($conn);  
?>  

output

FTP Buffer

Example 2

<?php  
    $conn= ftp_connect("127.0.0.1") or die("could not connect");  
    echo $conn;  
?>  

output

Resource id #2

Example 3

<?php  
    $handle = fopen("tpoint.txt", "r");  
    var_dump($handle);  
    echo "<br>";  
    $conn= ftp_connect("127.0.0.1") or die("could not connect");  
    var_dump($conn);  
?>  

output

resource(3, stream)

resource(4, FTP Buffer)


Null Data Type:

A variable of type Null is a variable without any data. In PHP, null is not a value, and we can consider it as a null variable based on 3 condition.

  1. If the variable is not set with any value.
  2. If the variable is set with a null value.
  3. If the value of the variable is unset.

Example 1

<?php  
       
    $empty=null;  
var_dump($empty);  
?> 

output

null

Example 2

<?php  
        $a1 = " ";  
     var_dump($a1);  
        echo "<br />";  
        $a2 = null;  
        var_dump($a2);  
?>  

output

strring ‘ ‘ (length=1)

null

Example 3

<?php  
    $x = NULL;  
    var_dump($x);  
    echo "<br>";  
    $y = "Hello javatpoint!";  
    $y = NULL;  
    var_dump($y);  
?>  

output

null

null

PHP is_null() function

By using the is_null function, we can check whether the variable is NULL or not. This function was introduced in PHP 4.0.4.

SYNATX:

  1. bool is_null ( mixed $var )  

Parameter

ParameterDescriptionIs compulsory
varThe variable being evaluated.compulsory

Return Type:

The PHP is_null() function returns true if var is null, otherwise false.

Important Note:

We can unset the variable value by using the unset function.

Example 1

<?php  
    $var1 = TRUE;  
    if (is_null($var1))  
        {  
            echo 'Variable is  NULL';  
        }  
        else  
        {  
            echo 'Variable is not NULL';  
        }  
?>  

output

Variable is not NULL

Example 2

<?php  
    $x= 100;  
    unset($x);  
    echo is_null($x);  
?>  

output

1

Example 3

<?php    
    $x = NULL;   
    $y = "\0";  
    is_null($x) ? print_r("True\n") : print_r("False\n");  
    echo "<br/>";  
    is_null($y) ? print_r("True\n") : print_r("False\n");  
?>  

output

Ture

False

Compound Types

There are 2 compound data types in PHP.

  1. Array
  2. Object

Array:

The array is a collection of heterogeneous (dissimilar) data types. PHP is a loosely typed language that?s why we can store any type of values in arrays.

Normal variable can store single value, array can store multiple values.

The array contains a number of elements, and each element is a combination of element key and element value.

SYNTAX OF ARRAY DECLARATION:

  1. Variable_name = array (element1, element2, element3, element4……)  

Example 1

  

<?php  
    $arr= array(10,20,30);  
    print_r($arr);  
?>

output

Array([0] => 10[1] =>20[2] => 30)

Example 2

<?php  
    $arr= array(10,'sonoo',30);  
    print_r($arr);  
?>  

output

Array([0] => 10[2] => sonoo[3] => 30)

Example 3

 

<?php  
    $arr= array(0=>10,2=>'sonoo',3=>30);  
    print_r($arr);  
?>

output

Array([0] => 10[2] => sonoo[3] => 30)


Object:

An object is a data type which accumulates data and information on how to process that data. An object is a specific instance of a class which delivers as templates for objects.

SYNTAX:

At first, we must declare a class of object. A class is a structure which consists of properties and methods. Classes are specified with the class keyword. We specify the data type in the object class, and then we use the data type in instances of that class.

Example 1

<?php class vehicle  
    {  
        function car()  
        {           
        echo "Display bmw motors";  
        }  
    }  
    $obj1 = new vehicle;  
    $obj1->car();   
?> 

output

Display bmw motors

Example 2

<?php  
    class student   
    {  
            function student()   
        {  
                $this->kundan = 100;  
            }  
    }     
    $obj = new student();  
    echo $obj->kundan;  
?> 

output

100

Example 3

<?php  
    class greeting  
    {  
    public $str = "Hello javatpoint and SSSIT";  
        function show_greeting()  
        {  
                return $this->str;  
            }  
    }  
    $obj = new greeting;  
    var_dump($obj);  
?>  

output

object(greeting([1]

public ‘str’ => string ‘Hello protutorials and tech’ (length=27)