PHP - Beginner
Hello World
<?php
echo "Hello World";
?>
echo "Hello World";
?>
Alternative in HTML
<script language="php">
</script>
echo "Hello World";
</script>
echo "Hello World";
if supported do this
<?
echo "Hello World";
?>
General things
<?php
echo "<strong>bold</strong>";
?>
or
<?php
echo "A";
echo "B";
echo "C";
?>
echo "<strong>bold</strong>";
?>
or
<?php
echo "A";
echo "B";
echo "C";
?>
Comments
//single line comment
/*
multi line
comment
*/
multi line
comment
*/
Variables
$variablename=value;
- must start with letter or underscore _VarName
- can contain alphanumeric char A-Z, 0-9,
- exemple
- variable_name
- variable_12
- variable name are case sensitive
- $name <>$NAME
- PHP auto converts variable to the correct data type
<?php
$name = `John`;
$age =25;
echo $name;
?>
$name = `John`;
$age =25;
echo $name;
?>
Variables scope
Var can be declared anywhere
Scope of variable
Part of script that can be referenced
Most used variable scopes
local scope
declared within a funcion . Can be accessed within that function
global
declared outside a function. Can be accessed outside of a function. Id declared within a function and for accessing it outside of a function use 'global'.
Exemple:
<?php
$name=`David`;
function getName(){
global $name;
echo name;
}
getName();
//outputs `David`
?>
?>
Variable variables
Using one variable to specify another variable`s name
Exemple:
<?php
$a=`hello`;
$hello=`Hi`;
echo $$a;
//outputs `Hi`
?>
Data Types
Data type supported: string, Integer, float, Boolean, Array, Object, Null, Resource
Most of data types can be combined:
<?php
$str="10";
$int=20;
$sum=$str+$int;
echo($sum);
//outputs 30
?>
?>
String
$string="Hello World";
Integer
$integer=42 // or -42
float
$float=42.168 // decimal
Boolean
$boolean=true;
Operators
Carry out operations on variables and values
+
=
-
*
/
% (remainder of division)
Exemple
<?php
$num1=8;
$num2=6;
echo $num1+$num2; // 14
?>
In modules
float numbers (decimal) are converted to integer before operation
<?php
$x=14;
$y=3;
echo $x%$y; // 2
?>
?>
Operands
1 3 500 1245 12.5
Increment & Decrement
increment or decrement a variable value
Normal use
$x++; // it is equivalent to $x=$x+1
$x--; // it is equivalent to $x=$x-1
post increment
It executes the code and return the value
Returns original value before changing
$a=2; $b=$a++; //$a=3, $b=2
pre increment
It executes the code and return the value
$a=2; $b=++$a; //$a=3, $b=3
Assignment operators
write values to variable
$num1=5;
$num2=$num1;
Concept:
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
Exemple
<?php
$x=50;
$x+=100; // it means $x+100 <=> 50 + 100 <=> 150
echo $x; // outputs 150
?>
Note
= means assignment (TR; değer atamak)
= = means comparison (TR: karşılaştırmak)
Comparison operators
== equal $x==$y if equal / true
=== identical $x===$ if equal and same type / true
!= or <> not equal $x!=$y or $x<>$y if not equal / true
!== not identical $x!==$y if not equal or not same type / true
> greater than $x>$y if great / true
< less than
>= geater or equal
<= less or equal
Logical operators
and $x and $y if both are true / true
or $x or $y either x or y true / true
xor $x xor $y either x or y true but not both / true
&& and $x && $y both are true / true
|| or $x || $y either x or y true / true
! not !$x $x is not true / true
Comments
Post a Comment