Example : How to make Comment in Php ?

<!DOCTYPE html>
<html>
	<body>
	  <?php 
	      // echo "Display Message in Php 1"; 
	  ?>
	</body>
</html> 
-----------------  OR  Single Line Comment----------------
<!DOCTYPE html>
<html>
	<body>
	  <?php 
	      # echo "Display Message in Php 2"; 
	  ?>
	</body>
</html> 

-----------------  OR  Multiline Line Comment----------------
<!DOCTYPE html>
<html>
	<body>
		<?php
                    /* echo "Display Message in Php 1";
                       echo "Display Message in Php 2";
                       echo "Display Message in Php 3";
                       echo "Display Message in Php 4"; */
                ?>
	</body>
</html>

Example : How to Print/Display message in Php?

<!DOCTYPE html>
<html>
	<body>
	    <?php echo "Display Message in Php 1"; ?>
          //OR  <?php print "Display Message in Php 1"; ?>
	</body>
</html> 
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>
	   <?php 
		echo "<h2> Message Printing in Php</h2>";
		echo "Display Message in Php 1";
                print "Display Message in Php 2"; 
	   ?>
	</body>
</html> 
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>
	   <?php 
		$msg="Message Printing in Php";
		echo "<h2> $msg </h2>";
		echo "Display Message in Php 1";
                print "Display Message in Php 2"; 
	   ?>
	</body>
</html>  
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>
	   <?php 
		$msg="Message Printing in Php";
		echo "<h2>". $msg . "</h2>";
		echo "Display Message in Php 1";
                print "Display Message in Php 2"; 
	   ?>
	</body>
</html>  
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>	
	  <?php 
	     echo "Display Message in Php 1<br>"; 
	     ECHO "Display Message in Php 2"; 
             eCHO "<br>";
	     eChO "Display Message in Php 3";
	  ?>
	</body>
</html> 
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>	
	  <?php 
	     echo "Display Message in Php 1<br>"; 
	     ECHO "Display Message in Php 2<br>"; 
	     eChO "Display Message in Php 3";
	  ?>
	</body>
</html>
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>	
	  <?php
		$x=10;
		$y=25;
		$z=$x+$y;	  
	    echo $x,"<br>"; 
	    ECHO $y,"<br>";
		echo $z;       
	  ?>
	</body>
</html>
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
 <body>	
  <?php	
    echo "Title of the Project is \"Online Travel Management System.\"<br>";
    echo 'Title of the Project is \'Online Travel Management System.\'';
  ?>
 </body>
</html> 

Output : 
Title of the Project is "Online Travel Management System."
Title of the Project is 'Online Travel Management System.'

NB: Php is not case sensitive

Example : How to use New Line in Php ?

<!DOCTYPE html>
<html>
	<body>	
	  <?php 
	     echo "Display Message in Php 1<br>"; 
	     ECHO "Display Message in Php 2"; 
             eCHO "<br>";
	     eChO "Display Message in Php 3";
	  ?>
	</body>
</html> 

Output :
Display Message in Php 1
Display Message in Php 2
Display Message in Php 3
-----------------  OR  ----------------
!DOCTYPE html>
<html>
	<body>	
	  <?php
		 echo "Welcome U all <br> in Your Site Codershelpline";
	  ?>
	</body>
</html> 
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
   <body>	
     <?php
        echo nl2br("Php function nl2br() along with \n and \r\n gives the new line on the browser window.");
     ?>
   </body>
</html> 

Output :
Php function nl2br() along with
and
gives the new line on the browser window.

Example : How do we declare variables in Php ?

<!DOCTYPE html>
<html>
	<body>
	
		<?php	
			$msg1 = "Hello India!";
			$msg1 = 'Hello India!';

			$msg2 = 410;			
			$msg3 = 3.141;
                        $msg4 = null;
                        $msg5 = true;
                        $msg6 = false;
                        $msg7 = array("Hello","India","World");
		?>
	</body>
</html> 

Example : How do we Concatenate String/Variable in Php ?

<!DOCTYPE html>
<html>
	<body>	
		<?php	
			$msg1 = 'Raman';
			$msg2 = "Hi $msg1";
			echo $msg2;
		?>
	</body>
</html>
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>	
		<?php	
			$msg1 = 'Raman';
			$msg2 = "Hi". $msg1;
			echo $msg2;
		?>
	</body>
</html>  
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>	
		<?php	
			$msg1 = 'Welcome';
			$msg2 = "India";
			echo $msg1." ".$msg2;

			$msg3=$msg1." ".$msg2;
			echo "<br>";
			echo $msg3;
		?>
	</body>
</html> 

Output :
Welcome India
Welcome India
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>	
	   <?php	
		$val = 15;
		//echo "Output is =: " . $val+35;  Error due to 35 is 
                                                   treated as non-numeric
		echo "Output is = " . ($val+35);	
	   ?>
	</body>
</html> 
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>	
		<?php			
			$msg1 = 'Welcome';
			$msg1.= " India";
			echo $msg1;			
		?>
	</body>
</html> 

Example : How to know the Datatype of declared Variable in Php?

<!DOCTYPE html>
<html>
	<body>
	   <?php 			
		$val = 3.141;
		var_dump($val);   // Php Function to check Datatype
	   ?>
	</body>
</html>
 
Output :
   float(3.141)
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>
	   <?php
		$val = 3141;
		var_dump(is_int($val));// Php Function to check int Datatype

		echo "<br>";
			
		$num = 3.141;
		var_dump(is_int($num));
		?>
	</body>
</html> 

Output :
  bool(true)
  bool(false)

NB: 
var_dump() function returns the data type and value.
is_int(), is_float()- returns int and float value true/false.
is_nan()- returns not a number true/false.
is_numeric() - returns a number is numeric,true/false.

Example : How to Typecast data in Php?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		$val = 3.141;
		$num= (int)$val;

		echo $num;
	   ?>
	</body>
</html>

Output :
3

Example : How to display Largest/Maximum value in Php ?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		echo(max(100,250,3,5,74,-121,532));
	   ?>
	</body>
</html>

Output :
532

Example : How to display Smallest/Minimum value in Php ?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		echo(min(100,250,3,5,74,-121,532));
	   ?>
	</body>
</html>

Output :
-121

Example : How to display Absolute value in Php ?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		echo(abs(-251));
	   ?>
	</body>
</html> 

Example : How to display Square Root value in Php ?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		echo(sqrt(25));
	   ?>
	</body>
</html> 

Example : How to Rounding Floating Point value in Php ?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		echo(round(25.954));
		echo "<br>";
		echo(round(25.324));
		?>
	</body>
</html> 

Output :
26
25

Example : How to generate Random Number value in Php ?

<!DOCTYPE html>
<html>
	<body>
	   <?php
	       echo(rand());  // To generate any random number.	
               echo(rand(23, 251)); // To generate random number bet 23-251.	
	   ?>
	</body>
</html> 

Example : What is Spaceship Operator(<=>) in Php ?

<!DOCTYPE html>
<html>
	<body>	
		<?php			
			echo 100 <=> 100,"<br>"; 
			echo 100 <=> 200,"<br>"; 
			echo 200 <=> 100; 				
		?>
	</body>
</html>
Output :
0
-1
1

 463 total views,  2 views today

Categories: Php

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.