1.Variables

Transkript

1.Variables
3. HAFTA DERS
1.Variables
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:



local
global
static
Global
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
Local
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
Global in Function1
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
Global in Function2
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>
PHP The static Keyword
Normally, when a function is completed/executed, all of its variables are deleted.
However, sometimes we want a local variable NOT to be deleted. We need it for a
further job.
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
2.Constants
Constants are like variables except that once they are defined they cannot be changed or
undefined
Syntax :
define(name, value, case-insensitive)
Parameters:



name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be caseinsensitive. Default is false
<?php
define("GREETING", "Welcome to W3Schools.com!", true);
echo greeting;
?>
Constants are Global
<?php
define("GREETING", "Welcome to W3Schools.com!");
function myTest() {
echo GREETING;
}
myTest();
?>
3.Operators







Arithmetic operators /Aritmetic
Assignment operators / Atama
Comparison operators / Karşılaştırma
Increment/Decrement operators / Artırma Eksiltme
Logical operators / Mantıksal
String operators / String
Array operators / Dizi
Arithmetic Operators
Operator
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 and $y
%
Modulus
$x % $y Remainder of $x divided by $y
**
Exponentiation $x ** $y
Modunu alma
Result of raising $x to the $y'th Üssünü alma
Assignment Operators
Assignment
x=y
Same as...
x=y
Description
The left operand gets set to the value of the expression on the right
x += y x = x + y
Toplama
Addition
x -= y
x=x–y
Çıkarma
Subtraction
x *= y x = x * y
Çarpma
Multiplication
x /= y x = x / y
Bölme
Division
x %= y x = x % y
Modunu alma Modulus
Comparison Operators
Operator
Name Example
Result
==
Equal
Değeri Eşittir
$x == $y
Returns true if $x is equal to $y
===
type
Identical
Tipi Eşittir
$x === $y
Returns true if $x is equal to $y, and they are of the same
!=
Not equal
Eşit Değil
$x != $y
Returns true if $x is not equal to $y
<>
Not equal
Eşit Değil
$x <> $y
Returns true if $x is not equal to $y
!==
Not identical
same type
Tipi Eşittir değil $x !== $y
Returns true if $x is not equal to $y, or they are not of the
>
Greater than
$x > $y
Returns true if $x is greater than $y
<
Less than
$x < $y
Returns true if $x is less than $y
>=
Greater than or equal to
$x >= $y
Returns true if $x is greater than or equal to $y
<=
Less than or equal to
$x <= $y
Returns true if $x is less than or equal to $y
Increment Artırma / Decrement Eksiltme Operators
Operator
Name
Description
++$x
Pre-increment
Increments $x by one, then returns $x
$x++
Post-increment
Returns $x, then increments $x by one
--$x
Pre-decrement
Decrements $x by one, then returns $x
$x--
Post-decrement
Returns $x, then decrements $x by one
$a=5;
$b=6;
$c= $a+ $b++ -> $c=11 $b=7;
$c= $a+ ++$b -> $c=12 $b=7;
Logical Operators
Operator
Name Example
Result Show it
and
ve
And
$x and $y
True if both $x and $y are true
&&
ve
And
$x && $y
True if both $x and $y are true
or
veya
Or
$x or $y
True if either $x or $y is true
||
veya
Or
$x || $y
True if either $x or $y is true
True true -> true
True false -> true
False true ->true
xor
Xor
$x xor $y
True false -> true
True if either $x or $y is true, but not both
karşılaştırmanın her iki tarafı doğru olmamalı sadece birinin doğru olması gerekir
False true ->true
!
Not
!$x
True if $x is not true
String Operators
Operator
Name
Example
Result
Concatenation of $txt1 and $txt2
.
Concatenation
$txt1 . $txt2
.=
Concatenation assignment
$txt1 .= $txt2 Appends $txt2 to $txt1
$a=”Ali”;
$b=”Can”;
.
.
$a=$a $b -> $a=”Ali Can” olur yada $a =$b -> $a=”Ali Can” olur
Array Operators DİZİ
Operator
Name
Example
Result Show it
+
Birleştirme Union
$x + $y Union of $x and $y
==
Equality
Returns true if $x and $y have the same key/value pairs
$x == $y
===
Identity
$x === $y
and of
the same types
Returns true if $x and $y have the same key/value pairs in the same order
!=
Inequality
$x != $y
Returns true if $x is not equal to $y
<>
Inequality
$x <> $y
Returns true if $x is not equal to $y
!==
Non-identity
$x !== $y
Returns true if $x is not identical to $y
4.if...else...elseif Statements
Syntax
if (condition) {
code to be executed if condition is true;
}
if statement - executes some code if one condition is true
if...else statement - executes some code if a condition is true and another code if that condition is false
if...elseif....else statement - executes different codes for more than two conditions
<?php
$t = date("H");
if ($t < "10")
{
echo "Have a good morning!";
}
elseif ($t < "20")
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
?>
5.switch Statement
Syntax
switch (n) {
case label1:
code to be
break;
case label2:
code to be
break;
case label3:
code to be
break;
...
default:
code to be
}
executed if n=label1;
executed if n=label2;
executed if n=label3;
executed if n is different from all labels;
Example
<?php
$favcolor = "red";
switch ($favcolor)
case "red":
echo "Your
break;
case "blue":
echo "Your
break;
case "green":
echo "Your
break;
default:
echo "Your
}
?>
{
favorite color is red!";
favorite color is blue!";
favorite color is green!";
favorite color is neither red, blue, nor green!";
6.PROGRAM KODLARI
<!DOCTYPE html>
<html>
<head> <title>Php Ders2</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-9″ />
</head>
<body>
<?php
/*
Değişkenler
local
global
static
*/
echo "<br>__________Global kullanımı_______<br>";
$x=5;
function test()
{
echo "Fonksiyon içinde değeri :".$x."<br>";
}
test();
echo "Fonksiyon dışında değeri:".$x."<br>";
echo "<br>__________Local kullanımı_______<br>";
function test2()
{
$y=5;
echo "Fonksiyon içinde değeri :".$y."<br>";
}
test2();
echo "Fonksiyon dışında değeri:".$y."<br>";
echo "<br>__________Global çağrılması 1_______<br>";
$a=5;
$b=10;
function test3()
{
global $a,$b; // Global değişkenlerin çağrılması,
$a=$a+$b;
echo "İşlemin Sonucu :".$a."<br>";
}
test3();
echo "<br>__________Global çağrılması 2_______<br>";
$k=5;
$l=10;
function test4()
{
$m = $GLOBALS['k'] + $GLOBALS['l']; // Global değişken çağrılması
echo "İşlemin Sonucu :".$m."<br>";
}
test4();
// **************** STATIC DEĞİŞKEN KULLANIMI****
// Fonksiyon içindeki local değişkenlerin son halini tutar
echo "<br>__________satatic değişken kullanımı_______<br>";
function test5()
{
static $n=0;
$n++;
echo "n değeri = $n <br>";
}
test5();
test5();
test5();
test5();
// *******************CONSTANT KULLANIMI******************
// Pogram içerisinde bir kere tanımlanıp değeri değişmeyen sabitlerdir
// syntax : define("adı","değeri", true/false büyüj küçük harf duyarlılığı)
echo "<br>__________CONSTANT KULLANIMI_______<br>";
define("dataserver","data.osym.gov.tr",true);
echo "Data server adresi :".dataserver."<br>";
echo "Data server adresi :".DataServer."<br>";
echo "<br>";
?>
</body>
</html>

Benzer belgeler