php and javascript
php and javascript
Definition
JavaScript is a scripting language which is done on a client-side. The various browser supports
JavaScript technology. DHTML uses the JavaScript technology for accessing, controlling, and
manipulating the HTML elements. The statements in JavaScript are the commands which tell the
browser for performing an action.
JavaScript is a dynamic computer programming language. It is lightweight and most commonly
used as a part of web pages, whose implementations allow client-side script to interact with the
user and make dynamic pages. It is an interpreted programming language with object-oriented
capabilities.
History
Javascript was invented by Brendan Eich in 1995. It was developed for netscape 2, and became
the ECMA-262 standard in 1997. After netscape handed javascript over to ECMA, the mozilla
foundation continued to develop javascript for the firefox browser. Mzilla’s latest version
1.8.5.(Identical to ES5).
Javascript variables
There are some rules while declaring a JavaScript variable (also known as identifiers).
Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
After first letter we can use digits (0 to 9), for example value1.
JavaScript variables are case sensitive, for example x and X are different variables.
Correct JavaScript variables
var x = 10;
var _value="
sonoo";
Incorrect JavaScript variables
var 123=30;
var *aa=320;
Example:-
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
Javascript Variable Scope
The scope of a variable is the region of your program in which it is defined. JavaScript
variables have only two scopes.
1. Global Variables: A global variable has global scope which means it can be defined
anywhere in your JavaScript code.
2. Local Variables: A local variable will be visible only within a function where it is
defined. Function parameters are always local to that function.
Javascript Datatypes
One of the most fundamental characteristics of a programming language is the set of data
types it supports. These are the type of values that can be represented and manipulated
in a programming language.
JavaScript allows you to work with three primitive data types:
Numbers, e.g., 123, 120.50 etc.
Strings of text e.g. "This text string" etc.
Boolean e.g., true or false.
Javascript Operators
Operator is a symbol that operates on given data and the variable, that takes action on
operands.
Let us take a simple expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and
‘+’ is called the operator. JavaScript supports the following types of operators.
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
Arithmetic operators:-
Arithmetic operators perform arithmetic operation on numbers (literals or variables).
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus(Remainder)
** Exponentiation
The exponentiation operator (**) raises the first operand to the power of
the second operand.
Let z=x**2;
X**y produces the same results as
math.pow(x,y)
let z=math.pow(x,2);
++ Increment
-- Decrement
Comparison Opearators
The JavaScript comparison operator compares the two operands. The comparison
operators are as follows:
Operator Description Example
== Is equal to 10==20=false
=== Identical(equal and of same type) 10==20=false
!== Not equal to 10!=20=true
!== Not Identical 20!==20=false
> Greater than 20>10=true
>= Greater than or equal to 20>=10=true
< Less than 20<10=false
<= Less than or equal to 20<=10=false
Logical operators
The following operators are known as JavaScript logical operators.
Operator Description Example
&& Logical AND (10==20&&20==33)=false
|| Logical OR (10==20||20==33)=false
! Logical NOT !(10==20)=true
Assignment Operators
Assignment operators assign values to JavaScript variables.
Operator Example Same as
= X=Y X=Y
+= X+=Y X=X+Y
-= X-=Y X=X-Y
*= X*=Y X=X*Y
/= X/=Y X=X/Y
%= X%=Y X=X%Y
**= X**=Y X=X**Y
PROGRAMS
1. WAP to find average of the numbers.
<HTML>
<HEAD>
<TITLE>finding average</TITLE>
</HEAD>
<BODY>
<script language=”javascritp” type =”text/javascript”>
Var a,b,c,avg;
a=10;
b=20;
c=15;
avg=(a+b+c)/3;
document.write(avg);
</script>
</BODY>
</HTML>
2. WAP to find greatest number between two different number using javascript.
<Html>
<Head>
<Title>finding greatest number</title>
</Head>
<Body>
<Script language=”javascript” type=”text/javascript”>
Var a,b;
a=5;
b=10;
if(a>b)
{
document.write(greatest number is”+a);
}
Else
{
Document.write(greatest number is”+b);
}
</Script>
</Body>
</Html>
PHP
Definition
The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers
to create dynamic content that interacts with databases. PHP is basically used for developing
web-based software applications. PHP started out as a small open-source project that evolved as
more and more people found out how useful it was. Rasmus Lerdorf unleashed the first version
of PHP way back in 1994.
Advantage
Free to use
Easy to learn
Compatible
Efficient
Secure
Database Support
Extensive library
Cloud Compatible
Variable Declaration
A variable can have a short name (like x and y) or a more descriptive name ( age,
carname,tota_volume).
Rules for PHP variables:-
A vriable starts with the $ sign, followed by the name of the variable.
A variable name must starts with a letter or the underscore character.
A variable name cannot starts with a number.
A variable name can only contain alpha-numeric character and underscore (a-z,0-9,and _)
Variable names are case-sesitive($age and $AGE are two different variable)
Datatype
Variable can store data of different types, and diferent data types can do different things,PHP
supports the following data types:
PHP String
PHP Integer
PHP Float(floating point numbers – also called double)
PHP Boolean
PHP Array
PHP Object
PHP NULL
PHP resource
Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
Arthmetic operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication etc.
Operators Name Example Result
+ Addition $x+$y Sum of $x and $y
- Subtraction $x-$y Difference of $x and $y
* Multiplication $x*$y Product of $x and $y
/ Division $x/$y Quotient of $x nd $y
% Modulus $x%$y Remainder of $x divided by $y
** Exponentiation $X**$y Result of raising $x to the $y’th
power
Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable. The
basic assignment operator in PHP is "=". It means that the left operand gets set to the
value of the assignment expression on the right.
Assignmment Same as Description
x=y x=y The left operand gets to the value of the
expression on the right
X+=y x=x+y Addition
x-=y x=x-y Subtraction
X*=y x=x*y Multiplication
x/=y x=x/y Division
X%=y x=x%y modulus
Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
Operators Name Example Results
== Equal $x==$y Returns true if $x is equal to $y
=== Identical $x===$y Return true if $x is equal to $y,and
they are of the same type
!= Not equal $x!=$y Return true if $x is not equal to $y
<> Not equal $x<>$y Return true if $x is not equal to $y
!== Not identical $x!==$y Return true if $x is not equal to $y, or
they are not of the same type
> Greater than $x>$y Return true if $x is greater than $y
< Less than $x<$y Return true if $x is less than $y
>= Greater than or equal to $x>=$y Return true if $x is greater than or
equal to $y
<= Less than or equal to $x<=$y Return true if $x is less than or equal
to $y
Spaceship $x$y Return an integer less than,equal to,or
greater than zero, depending on iff $x
is less than ,equal to,or greater than
$y,introduced inn PHP 7.
Increment/decrement operators
The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
operators Name Description
++$x Pre-increment Increment $x by one, then returns $x
$x++ Post-increment Return $x,then increment $x by one
--$x Pre-decrement Decrement $x by one, then return $x
$x-- Post-decrement Return $x, then decrement $x by one
Logical operators
The PHP logical operators are used to combine conditional statements.
Operator Name Example Result
s
And And $x and $y True if both $x and $y are true
Or Or $x or $y True if either $x or $y is true
Xor Xor $x xor $y True if either $x or $y is true,but not both
&& And $x &&$y True if both $x and $y are true
|| Or $x||$y True if either $x or $y is true
! Not !$x True if $x is not true
String operator
PHP has two operators that are specially designed for strings.
Operator Name Example Result
. Concatenation $txt1.$txt2 Concatenation of $txt1 and $txt2
.= Concatenation $txt1.=$txt2 Appends $txt2 to $txt1
assignment
Program
1. WAP to find sum of two number using PHP.
<Html>
<Head>
<Title>to find sum</Title.
</Head>
<Body>
<? Php
$a=5;
$b=10;
$c=$a+$b;
echo”sum is $c”;
?>
</Body>
</Html>
Arthmetic operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication etc.
Operators Name Example Result
+ Addition $x+$y Sum of $x and $y
- Subtraction $x-$y Difference of $x and $y
* Multiplication $x*$y Product of $x and $y
/ Division $x/$y Quotient of $x nd $y
% Modulus $x%$y Remainder of $x divided by $y
** Exponentiation $X**$y Result of raising $x to the $y’th
power
Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable. The
basic assignment operator in PHP is "=". It means that the left operand gets set to the
value of the assignment expression on the right.
Assignmment Same as Description
x=y x=y The left operand gets to the value of the
expression on the right
X+=y x=x+y Addition
x-=y x=x-y Subtraction
X*=y x=x*y Multiplication
x/=y x=x/y Division
X%=y x=x%y modulus
Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
Operators Name Example Results
== Equal $x==$y Returns true if $x is equal to $y
=== Identical $x===$y Return true if $x is equal to $y,and
they are of the same type
!= Not equal $x!=$y Return true if $x is not equal to $y
<> Not equal $x<>$y Return true if $x is not equal to $y
!== Not identical $x!==$y Return true if $x is not equal to $y, or
they are not of the same type
> Greater than $x>$y Return true if $x is greater than $y
< Less than $x<$y Return true if $x is less than $y
>= Greater than or equal to $x>=$y Return true if $x is greater than or
equal to $y
<= Less than or equal to $x<=$y Return true if $x is less than or equal
to $y
Spaceship $x$y Return an integer less than,equal to,or
greater than zero, depending on iff $x
is less than ,equal to,or greater than
$y,introduced inn PHP 7.
Increment/decrement operators
The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
operators Name Description
++$x Pre-increment Increment $x by one, then returns $x
$x++ Post-increment Return $x,then increment $x by one
--$x Pre-decrement Decrement $x by one, then return $x
$x-- Post-decrement Return $x, then decrement $x by one
Logical operators
The PHP logical operators are used to combine conditional statements.
Operator Name Example Result
s
And And $x and $y True if both $x and $y are true
Or Or $x or $y True if either $x or $y is true
Xor Xor $x xor $y True if either $x or $y is true,but not both
&& And $x &&$y True if both $x and $y are true
|| Or $x||$y True if either $x or $y is true
! Not !$x True if $x is not true
String operator
PHP has two operators that are specially designed for strings.
Operator Name Example Result
. Concatenation $txt1.$txt2 Concatenation of $txt1 and $txt2
.= Concatenation $txt1.=$txt2 Appends $txt2 to $txt1
assignment
Program
6. WAP to find sum of two number using PHP.
<Html>
<Head>
<Title>to find sum</Title.
</Head>
<Body>
<? Php
$a=5;
$b=10;
$c=$a+$b;
Echo”sum is $c”;
?>
</Body>
</Html>