PHP , Javascript (JS) Array Basics Create And Print

 
PHP , Javascript (JS) Array Basics Create And Print 


What is Array?

Array is a special type of variable which stores multiple value in a single variable by its different index number.

Graphical Array value Storage View.


How To Declare or Create in PHP and Javascript?

In PHP – $a=array(1,2,3,4,5);
PHP Create Array by Keyword array and value inside its parenthesis seperated by comma (,).
Here We can Access value of a by using its its like if we use $a[0] we get
value equal to 1. Since Zero index value is 1.

In Javascript – var a=[1,2,3,4,5];
Javascript Creates Array By using Square Bracket and value separated by comma (,)
Here We can Access value of a by using its its like if we use a[0] we get
value equal to 1. Since Zero index value is 1.

Example 1. How to Decalare and get value of array .

IN PHP

<?php
$a = array(1,2,3,4); //Creating array

//printing values of array using simple method 1
echo "Index 0 Value is" . $a[0]."</br>";
echo "Index 1 Value is" . $a[1]."</br>";
echo "Index 2 Value is" . $a[2];

//printing values of array using for loop method 2

$count=count($a); //Here Count Function Counts The Array Of Length

for($i=0;$i<$count;$i++)
{
echo "Index ".$i." Value is" . $a[$i]."</br>";
}

//printing values of array using foreach loop method 2

foreach($a as $key => $value)
{
echo "Index ".$key." Value is" . $value."</br>";
}
?>

IN Javascript

<html>
<body>
</body>
<script>
var a = [1,2,3,4]; //Creating array

//printing values of array using simple method 1
console.log( "Index 0 Value is" +a[0]);
console.log( "Index 1 Value is" +a[1]);
console.log( "Index 2 Value is" +a[2]);

//printing values of array using for loop method 2

var count=a.length; //Here .length Function Counts The Array Of Length

for(var i=0;i<count;i++)
{
console.log( "Index "+i+" Value is" +a[i]);
}

//printing values of array using foreach loop method 2

for(var key in a)
{
console.log("Index "+key+" Value is" +a[key]);
}
</script>
</html>

Thank You
9

Javascript Basics


Javascript Basics



Javascript is a client side popular language used in many things like in our website, Hybrid Mobile Application, etc.

In Earlier Javascript is used only in websites But now a days javascript is very and used in many devices.

What Javascript Can Do?

Javascript in Web Can Change HTML tags ,CSS Style, HTML Attributes, HTML Contents, HIDE Elements, Show Elements.

Javascript in Mobile Application Can Call Native Java Functions, Swift function , C# functions and Methods of different programing languages.

Where to Put javascript Codes?

Javascript can be Call in web Page by two methods : -

1. By using Internally
Javascript Codes Must be Inside in Script tag

Example :-

<html>
<head>
<!--Adding javascript in our page-->
<script>
alert(“Calling javascript”); //calling alert function
</script>
</head>
<body>
<h2>Javascript Basics</h2>
</body>
</html>

2.By using Externally

Example :-

page.html

<html>
<head>
<!--Adding javascript in our page-->
<script src=”script.js”></script>
</head>
<body>
<h2>Javascript Basics</h2>
</body>
</html>
script.js

alert(“Calling javascript”); //calling alert function


Note :- Javascript is must different from java

Javascript Outputs

JavaScript can display data in different Such Ways :
  • Writing into an alert box, using alert().
  • Writing into the HTML output using document.write().
  • Writing into an HTML element, using innerHTML.
  • Writing into the browser console, using console.log().


Using alert
<html>
<body>
<h2>Javascript basics</h2>

<script>
alert(Hello Js);
</script>

</body>
</html>


Using document.write
<html>
<body>
<h2>Javascript basics</h2>

<script>
document.write(Hello javascript);
</script>

</body>
</html>

Using document.write
<html>
<body>
<h2>Javascript basics</h2>

<script>
document.write(Hello javascript);
</script>

</body>
</html>

Using innerHTML

<html>
<body>

<h2>Javascript basics</h2>

<p id="display"></p>

<script>
document.getElementById("display").innerHTML = hello javascript;

//here document.getElementById is a selector which select the html elements by using id and change the text inside this tag
</script>

</body>
</html>

Using console.log

<html>
<body>

<h2>Javascript basics</h2>

<script>
console.log(hello javascript);

//open your browser console by pressing F12 Key

</script>

<
/body>
<
/html>

Javascript Syntax

Javascript Statements Must be End With (;) Semicolon

Javascript Use var keyword to Store and Declare variable

Javascript Concatinate using +

Example : var a=”hello”+” ”+”Js”

Javascript Comments
//This is single line comments
/*This is Multi Line comments */

Thank You
1