Array

Example : How the contents of an Index Array is Displayed in Php?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		$vehicles = array("Scootie", "Motorcycle", "Cars", "Truck");
			echo $vehicles[0];
			echo $vehicles[1];
			echo $vehicles[2];
			echo $vehicles[3];
	   ?>
	</body>
</html>
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>
	   <?php
	   
			$vehicles[0] ="Scootie";
			$vehicles[1] ="Motorcycle";
			$vehicles[2] ="Cars";
			$vehicles[3] ="Truck";		
		
			for ($i=0;$i<4;$i++)
			{
			   echo $vehicles[$i];
			}
	   ?>
	</body>
</html>
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>
	   <?php
	   
			$vehicles[0] ="Scootie";
			$vehicles[1] ="Motorcycle";
			$vehicles[2] ="Cars";					
			
			$len=count($vehicles);
			
			for ($i=0;$i<$len;$i++)
			{
				echo $vehicles[$i];
			}
	   ?>
	</body>
</html>

Example : How the contents of an Array is Count in Php?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		$vehicles = array("Scootie", "Motorcycle", "Cars", "Truck");
		echo count($vehicles);
	   ?>
	</body>
</html> 

Output:
4

Example : How the contents of an Array is Sort in Ascending Order in Php?

<!DOCTYPE html>
<html>
	<body>	   
	   <?php
	   
		$vehicles = array("Scootie", "Motorcycle", "Cars", "Truck");					
		sort($vehicles);
		$len=count($vehicles);			
		for ($i=0;$i<$len;$i++)
		{
			echo $vehicles[$i];
		}
	   ?>	
	</body>
</html> 
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>	   
	   <?php
	   
		$value = array("200", "410", "5", "-415");					
		sort($value);

		$len=count($value);			
		for ($i=0;$i<$len;$i++)
		{
		   echo $value[$i]." ";
		}
	   ?>	
	</body>
</html> 

Example : How the contents of an Array is Sort in Descending Order in Php?

<!DOCTYPE html>
<html>
	<body>	   
	   <?php
	   
		$vehicles = array("Scootie", "Motorcycle", "Cars", "Truck");					
		rsort($vehicles);
		$len=count($vehicles);			
		for ($i=0;$i<$len;$i++)
		{
			echo $vehicles[$i];
		}
	   ?>	
	</body>
</html> 
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>	   
	   <?php
	   
		$value = array("200", "410", "5", "-415");					
		rsort($value);

		$len=count($value);			
		for ($i=0;$i<$len;$i++)
		{
		   echo $value[$i]." ";
		}
	   ?>	
	</body>
</html>

Example : How the contents of an Associative Array is displayed in Php?

<!DOCTYPE html>
<html>
	<body>
		<?php
	   
			$vehicles['val'] ="500";
			$vehicles['num'] ="200";
			$vehicles['digit'] ="50";					
			
			echo $vehicles['val'];
			echo "  "; 
			echo $vehicles['num'];
			echo "  "; 
			echo $vehicles['digit'];			
		?>
	</body>
</html>

Output :
   500 200 50
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>
	   <?php   
			 
               $vehicles=array('val'=>"500",'num'=>"200",'digit'=>"50");
			
			echo $vehicles['val'];
			echo "  "; 
			echo $vehicles['num'];
			echo "  "; 
			echo $vehicles['digit'];			
           ?>
	</body>
</html>

Output :
   500 200 50
-----------------  OR  ----------------
<!DOCTYPE html>
<html>
	<body>
	   <?php   
			 
            $vehicles=array('val'=>"500",'num'=>"200",'digit'=>"50");
			
			foreach($vehicles as $m => $contents) 
			{
			echo "Value at " . $m . " Index is = " . $contents;
			echo "<br>";
			}			
           ?>
	</body>
</html>

Output :
Value at val Index is = 500
Value at num Index is = 200
Value at digit Index is = 50

String

Example : How to find the Length of a string/sentence in Php?

<!DOCTYPE html>
<html>
	<body>	
		<?php			
			echo strlen("Codershelpline is Your site");					
		?>
	</body>
</html>

Output :
27 

Example : How to find the number of Words in a string/sentence in Php ?

<!DOCTYPE html>
<html>
	<body>	
		<?php			
		   echo str_word_count("Codershelpline is Your site");					
		?>
	</body>
</html>

Output :
4
Example : How to reverse the given String in Php?
<!DOCTYPE html>
<html>
	<body>	
		<?php			
			echo strrev("Codershelpline");					
		?>
	</body>
</html>
Output : enilplehsredoC

Example : How to  Search a Word in a given string/sentence in Php ?

<!DOCTYPE html>
<html>
	<body>	
		<?php			
			echo strpos("Codershelpline.com","helpline");					
		?>
	</body>
</html>
Output :
6

Example : How to  Replace a Word in a given String/Sentence in Php ?

<!DOCTYPE html>
<html>
	<body>
	   <?php
		echo str_replace("help", "India", "Codershelpline"); 
	   ?>
	</body>
</html> 

Output :
CodersIndialine

 316 total views,  1 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.