├── README.md ├── learnphp1.php ├── learnphp2.php ├── learnphp3.php ├── learnphp4.php └── oop.php /README.md: -------------------------------------------------------------------------------- 1 | # Learn-PHP-codes 2 | helpful files to learn php 3 | -------------------------------------------------------------------------------- /learnphp1.php: -------------------------------------------------------------------------------- 1 | 2 | 12 | 18 | 29 | Get Type"; 33 | echo gettype($var1); 34 | echo "

variable dump

"; 35 | echo var_dump($var1); 36 | 37 | $var2=100; data type integer 38 | echo "

Get Type

"; 39 | echo gettype($var2); 40 | echo "

variable dump

"; 41 | echo var_dump($var2); 42 | 43 | $var3=true; data type boolean 44 | echo "

Get Type

"; 45 | echo gettype($var3); 46 | echo "

variable dump

"; 47 | echo var_dump($var3); 48 | 49 | $var4=10.5; data type float/double 50 | echo "

Get Type

"; 51 | echo gettype($var4); 52 | echo "

variable dump

"; 53 | echo var_dump($var4); 54 | 55 | 56 | $var5= array( data type array 57 | "A"=>"val1", 58 | "B"=>"val2", 59 | "C"=>"val3", 60 | ); 61 | echo "

Get Type

"; 62 | echo gettype($var5); 63 | echo "

variable dump

"; 64 | echo var_dump($var5); 65 | 66 | $var6=null; data type null 67 | echo "

Get Type

"; 68 | echo gettype($var6); 69 | echo "

variable dump

"; 70 | echo var_dump($var6); 71 | 72 | 73 | 74 | class book{ data type object 75 | function book(){ 76 | $this->genre="Adventure"; 77 | } 78 | } 79 | $var7= new book(); 80 | echo "

Get Type

"; 81 | echo gettype($var7); 82 | echo "

variable dump

"; 83 | echo var_dump($var7); 84 | 85 | 86 | 87 | $var8=fopen('names.txt','r'); data type resource 88 | echo "

Get Type

"; 89 | echo gettype($var8); 90 | echo "

variable dump

"; 91 | echo var_dump($var8); 92 | */ 93 | ?> 94 | 95 | $num2){ 115 | echo $num1. " is larger than ". $num2; 116 | } 117 | else if($num1==$num2){ 118 | echo $num1. " is equal to ". $num2; 119 | 120 | } 121 | else{ 122 | echo $num1." isnt larger than ". $num2; 123 | } 124 | 125 | 126 | 127 | 128 | $var=100; information for me 129 | $var +=200; 130 | echo $var; //output 300 131 | 132 | 133 | $name="moemen "; 134 | $name .= "saadeh"; //output moemen saadeh 135 | 136 | 137 | 138 | $var=10; 139 | echo ++$var //output 11 ++$var : here ++ first => direktly it will increase 1 then it will print it 140 | 141 | 142 | $var=10; 143 | echo $var++ ; //ouput 10 $var++ : here ++ second => first it will print the same number 144 | 145 | 146 | 147 | && 148 | xor 1 condition is true but not all 149 | 150 | 151 | 152 | $file= fopen("ali.txt","r"); //output : failed to open stream : no such file or directory in c :...... 153 | how can hid the error; just add @ operator before 154 | $file= @fopen("ali.txt","r"); nothing will appear ,the error wil ignore 155 | !!!! and if i want to write my custom error , i can like this 156 | $file= @fopen("ali.txt","r")or die("this file is not found"); 157 | 158 | 159 | */ 160 | 161 | 162 | /* for($i=1; $i<=20;$i++) { 163 | echo $i ."
"; 164 | 165 | 166 | } 167 | */ 168 | ?> 169 | 181 | "; 185 | 186 | for($i=1;$i<6;$i++){ 187 | echo "
  • ". $lang[$i] ."
  • "; 188 | } 189 | echo""; 190 | */ 191 | 192 | 193 | 194 | 195 | 196 | 197 | /* for loop 198 | the fisrt methode 199 | 200 | for($i=1; $i<=20;$i++) { 201 | echo $i ."
    "; 202 | 203 | 204 | } 205 | 206 | 207 | 208 | the second methode 209 | 210 | for($i=1;;$i++){ 211 | if($i>20){ 212 | break; 213 | } 214 | echo $i."
    "; 215 | } 216 | 217 | 218 | 219 | 220 | the third metode 221 | $i=1; 222 | for( ; ;$i++){ 223 | if($i>20){ 224 | break; 225 | } 226 | echo $i . "
    "; 227 | } 228 | 229 | 230 | 231 | the fourth methode 232 | $i=1; 233 | for(;;){ 234 | if($i>20){ 235 | break; 236 | } 237 | echo $i."
    "; 238 | $i++; 239 | } 240 | 241 | 242 | 243 | 244 | while loop 245 | first methode 246 | 247 | $i=1; 248 | while($i<=20){ 249 | echo $i++ ."
    "; 250 | } 251 | 252 | 253 | second methode 254 | $i=1; 255 | while($i<=20): 256 | echo $i++ ."
    "; 257 | endwhile; 258 | 259 | 260 | array 261 | 262 | $countries=array("misir","bahrain","lebanon","saudia","turkey","amerika"); 263 | foreach($countries as $value){ 264 | echo $value ."
    "; 265 | } 266 | 267 | 268 | 269 | $countries=array( 270 | "eg"=>"misir", 271 | "bah"=>"bahrain", 272 | "leb"=>"lebanon", 273 | "sau"=>"saudia", 274 | "tur"=>"turkey", 275 | "ame"=>"amerika"); 276 | 277 | foreach($countries as $key=> $value){ 278 | echo $key."=>". $value ."
    "; 279 | } 280 | 281 | function 282 | 283 | 284 | 285 | function hellosay(){ 286 | echo"hello php"; 287 | } 288 | hellosay(); 289 | 290 | 291 | 292 | 293 | function count1to3(){ 294 | for($i=1;$i<=30;$i++){ 295 | echo $i."
    "; 296 | } 297 | } 298 | count1to3(); 299 | 300 | 301 | 302 | 303 | 304 | function sayhello($name){ function with 1 argument 305 | echo "hello " . $name; 306 | } 307 | sayhello("moemen"); 308 | 309 | 310 | 311 | 312 | function sayhello($name,$age){ function with 2 argument 313 | echo "hello " . $name . "and my age is ". $age; 314 | } 315 | sayhello("moemen","18"); 316 | 317 | */ 318 | ?> 319 | 320 | "; 326 | for($lang=0;$lang". $languages[$lang] .""; 328 | } 329 | echo""; 330 | */ 331 | /* 332 | 333 | 334 | array associative 335 | 336 | 337 | $key => value 338 | * key must be unique : i cant use the same key but i can use the same value 339 | 340 | 341 | 342 | $languages = array( 343 | "html"=>"55%", 344 | "css" =>"95%", 345 | "js" =>"45%", 346 | "python" =>"25%", 347 | "ruby" =>"33%", 348 | "php" =>"56%", 349 | "arduino" =>"100%"); 350 | 351 | $languages["MySql"]= "30%"; 352 | 353 | echo ""; 358 | 359 | 360 | */ 361 | 362 | ?> 363 | "; 393 | print_r($array); 394 | echo ""; 395 | 396 | 397 | 398 | 399 | 400 | 401 | Array Methods - Remove Items ,how to remove element to an array 402 | 403 | ! array_pop : remove elements from the end of array 404 | ! array_shift: remove elements from the fist of array 405 | 406 | 407 | 408 | 409 | $array = array("html","css","js","python","ruby"); 410 | print_r($array); 411 | echo "
    ";
    412 | print_r($array);
    413 | echo "
    "; 414 | 415 | $lastlang = array_pop($array); 416 | echo "
    ";
    417 | print_r($array);
    418 | echo "
    "; 419 | 420 | echo $lastlang; 421 | 422 | 423 | */ 424 | 425 | ?> 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | -------------------------------------------------------------------------------- /learnphp2.php: -------------------------------------------------------------------------------- 1 | "; 12 | print_r($elements); 13 | echo""; 14 | sort($elements); 15 | echo"
    ";
     16 |    print_r($elements);
     17 |    echo"
    "; 18 | 19 | 20 | Array Methods - Sort Associative Array 21 | 22 | asort : ranking(value) from a to z and from 0 to ... 23 | arsort: ranking from z to a and from ... to 0 24 | !!!sort and rsort specifique for index array only 25 | 26 | 27 | ksort : ranking(key of Associative Array) from a to z and from 0 to ... 28 | krsort: ranking(key of Associative Array) from z to a and from ... to 0 29 | 30 | 31 | 32 | 33 | $elements= array( 34 | "html"=>54, 35 | "css"=> 70, 36 | "html5"=>45, 37 | "java"=>33, 38 | "php"=>93, 39 | ); 40 | 41 | echo"
    ";
     42 |             print_r($elements);
     43 |             echo"
    "; 44 | 45 | asort($elements); 46 | echo"
    ";
     47 |     print_r($elements);
     48 |     echo"
    "; 49 | 50 | 51 | Array Methods - Shuffle, Reverse 52 | 53 | 54 | array_reverse(where,true or false) : by default its false 55 | false : all elements in array it will be reverse with changing the key number ; 56 | true : all elements in array it will be reverse wihtout changing the key number ( every number will stay to own element) 57 | 58 | Shuffle(where): all elements in array it will be reverse but randomly where you do refresh to page, and all key will stay fix, from 0 to ... 59 | 60 | 61 | $elements= array("html","css","html5","java"); 62 | echo"
    ";
     63 |    print_r($elements);
     64 |    echo"
    "; 65 | $reverse= array_reverse($elements); 66 | echo"
    ";
     67 |    print_r($reverse);
     68 |    echo"
    "; 69 | 70 | 71 | Array Methods - Array Fill 72 | array_fill(index,how much,value); 73 | 74 | 75 | $array=array_fill(2,10,"moemen"); 76 | echo "
    ";
     77 |   print_r ($array);
     78 | echo "
    "; 79 | 80 | 81 | OUTPUT 82 | ------ 83 | Array 84 | ( 85 | [2] => moemen 86 | [3] => moemen 87 | [4] => moemen 88 | [5] => moemen 89 | [6] => moemen 90 | [7] => moemen 91 | [8] => moemen 92 | [9] => moemen 93 | [10] => moemen 94 | [11] => moemen 95 | ) 96 | 97 | 98 | 99 | $array=array_fill(2,10,array("moemen","ali","abed")); 100 | echo "
    ";
    101 |   print_r ($array);
    102 | echo "
    "; 103 | 104 | 105 | */ 106 | 107 | ?> 108 | "; 139 | echo $array[$rand[1]] ."
    "; 140 | echo $array[$rand[2]] ."
    "; 141 | 142 | 143 | 144 | 145 | 146 | Array Methods - Array Unique 147 | 148 | $array=array("moemen","ali","abed","moemen","khaled","abed"); 149 | echo "
    ";
    150 |   print_r ($array);
    151 | echo "
    "; 152 | 153 | $unique= array_unique($array); 154 | echo "
    ";
    155 |   print_r ($unique);
    156 | echo "
    "; 157 | 158 | output 159 | ------ 160 | before 161 | ------ 162 | Array 163 | ( 164 | [0] => moemen 165 | [1] => ali 166 | [2] => abed 167 | [3] => moemen 168 | [4] => khaled 169 | [5] => abed 170 | ) 171 | 172 | 173 | 174 | after 175 | ----- 176 | Array 177 | ( 178 | [0] => moemen 179 | [1] => ali 180 | [2] => abed 181 | [4] => khaled 182 | ) 183 | 184 | 185 | 186 | 187 | 188 | 189 | String Functions - Explode : string to array 190 | 191 | syntax("from where you want to cut the array",array name, limit) 192 | limit its optional , 193 | 194 | 195 | $str= "Hello i love php to much"; 196 | echo $str ."
    "; 197 | $arr= explode(" ",$str); 198 | 199 | echo "
    ";
    200 |   print_r ($arr);
    201 | echo "
    "; 202 | 203 | 204 | in this case 205 | 206 | $str= "Hello i love php to much"; 207 | echo $str ."
    "; 208 | $arr= explode(" ",$str,3); 209 | 210 | echo "
    ";
    211 |   print_r ($arr);
    212 | echo "
    "; 213 | 214 | output: 215 | ------ 216 | Hello i love php to much 217 | Array 218 | ( 219 | [0] => Hello 220 | [1] => i 221 | [2] => love php to much 222 | ) 223 | 224 | --------------------------------------------- 225 | 226 | in limit 0 and 1 its the same 227 | 228 | $str= "Hello i love php to much"; 229 | echo $str ."
    "; 230 | $arr= explode(" ",$str,-2); 231 | 232 | echo "
    ";
    233 |   print_r ($arr);
    234 | echo "
    "; 235 | 236 | 237 | output: 238 | ------ 239 | Hello i love php to much 240 | Array 241 | ( 242 | [0] => Hello 243 | [1] => i 244 | [2] => love 245 | [3] => php 246 | ) 247 | 248 | 249 | String Functions - Implode/join: array to string 250 | 251 | 252 | $str= array("moemen","ali","khaled","yousof"); 253 | $implode = implode(" & " ,$str); => the first parametre is to separate between elements of function (optional) 254 | 255 | or $implode = join(" & " ,$str); 256 | 257 | echo "Hello our moderators names is ". $implode ; 258 | 259 | 260 | 261 | 262 | String Functions - Str_Split, Chunk_Split 263 | 264 | syntax: str_split(name of string,length) => lenght by default its 1 265 | 266 | $str ="Hello i love php"; 267 | $splite= str_split($str,2); 268 | echo"
    ";
    269 | print_r ($splite);
    270 | echo"
    "; 271 | 272 | output: 273 | ------ 274 | Array 275 | ( 276 | [0] => He 277 | [1] => ll 278 | [2] => o 279 | [3] => i 280 | [4] => lo 281 | [5] => ve 282 | [6] => p 283 | [7] => hp 284 | ) 285 | 286 | 287 | -------------------- 288 | 289 | 290 | syntax: Chunk_Split(name of string,length,end) => lenght by default its 76 , end by default its \r or \n 291 | 292 | $str= "HelloIlovephp"; 293 | $chunk = chunk_split($str,4," @ "); 294 | echo $chunk; 295 | 296 | output 297 | ------ 298 | Hell @ oIlo @ veph @ p @ 299 | 300 | */ 301 | ?> 302 | but tihs optional 309 | 310 | $str="I love php so much because php its a good lang and php is famous"; 311 | $replace =str_replace("php","javascript",$str,$i); 312 | echo $replace."
    "; 313 | echo $i; 314 | 315 | 316 | advanced example 317 | ----------------- 318 | $str="I love php so much because php its a good lang and php is famous"; 319 | echo $str."
    "; 320 | $str = explode(" ",$str); 321 | echo "
    ";
    322 |  print_r ($str);
    323 | echo "
    "; 324 | 325 | $replace =str_replace("php","javascript",$str); 326 | echo "
    ";
    327 | print_r($replace);
    328 | echo "
    "; 329 | 330 | $implode=implode(" ",$replace); 331 | echo $implode; 332 | 333 | 334 | 335 | other example 1: 336 | ---------- 337 | if i want to replace many word in string 338 | 339 | $str="I love|php@so$much|because@php|its@a=good=lang@and|php=is|famous"; 340 | $str= str_replace(array("=","@","|")," ",$str); 341 | echo $str; 342 | 343 | output 344 | -------- 345 | I love php so much because php its a good lang and php is famous 346 | 347 | 348 | other example 2: 349 | ---------- 350 | if i want to replace many word in string 351 | 352 | $str="I love|php@so$much|because@php|its@a=good=lang@and|php=is|famous"; 353 | $str= str_replace(array("=","@","|"),array(" a "," b "," c "),$str); 354 | echo $str; 355 | 356 | output 357 | -------- 358 | I love c php b so much|because b php|its b a a good a lang b and c php a is c famous 359 | 360 | */ 361 | 362 | 363 | ?> 364 | "; 372 | $str= str_repeat($str,20); 373 | echo $str; 374 | 375 | ----------------------------- 376 | 377 | str_shuffle() 378 | 379 | it will mix the lettres randomly when i refresh 380 | 381 | $str ="moemen saade"; 382 | $shuffle= str_shuffle($str); 383 | echo $shuffle; 384 | 385 | ------------------------------- 386 | 387 | strlen() 388 | 389 | $str ="moemen"; 390 | $shuffle= strlen($str); 391 | echo $shuffle; 392 | 393 | !! index starts from 0 394 | !! length starts from 1 395 | 396 | output 397 | ------ 398 | 6 399 | 400 | */ 401 | 402 | ?> -------------------------------------------------------------------------------- /learnphp3.php: -------------------------------------------------------------------------------- 1 | "; 14 | echo addslashes($str) . " This is safe in a database query."; 15 | 16 | output: 17 | Who's Peter Griffin? This is not safe in a database query. 18 | Who\'s Peter Griffin? This is safe in a database query. 19 | 20 | String Functions - Stripslashes 21 | 22 | stripslashes : to remove the slashes 23 | echo stripslashes("Who\'s Peter Griffin?"); 24 | Who's Peter Griffin? 25 | 26 | 27 | String Functions - AddSlashes, Strip_Tags 28 | remove tags 29 | syntax: strip_tags(string,allow) /// allow : the tags that i want to leave it without removing 30 | 31 | echo strip_tags("Hello >world!",""); 32 | 33 | 34 | String Functions - StrToLower, StrToUpper 35 | 36 | 37 | 38 | $normalSting = "moemen saadeh"; 39 | echo $upper = strtoupper($normalSting)."
    "; 40 | 41 | echo $upper= strtolower($upper); 42 | 43 | String Functions - lcfisrt, ucfisrt ,ucwords 44 | 45 | lcfirst(): lowercase first 46 | ucfirst(): uppercase first 47 | ucwords(); the first lettres of all words, it will be capital 48 | 49 | $normalSting = "Moemen saadeh"; 50 | echo $lcf= lcfirst($normalSting)."
    "; 51 | output : moemen saadeh 52 | 53 | ------------------------ 54 | 55 | $normalSting = "moemen saadeh"; 56 | echo $lcf= upfirst($normalSting)."
    "; 57 | 58 | output : Moemen saadeh 59 | 60 | ---------------------- 61 | 62 | $normalSting = "Moemen saadeh"; 63 | echo $ucwords= ucwords($normalSting)."
    "; 64 | 65 | output : Moemen Saadeh 66 | 67 | 68 | String Functions - Trim 69 | 70 | trim : trim from right and left 71 | ltrim: trim from right only 72 | rtrim: trim from left only 73 | 74 | example 75 | 76 | $str =" i love php so much "; 77 | var_dump($str); 78 | echo "
    "; 79 | $trimmed = ltrim($str); 80 | var_dump($trimmed); 81 | 82 | output: 83 | ---------- 84 | string(21) " i love php so much " 85 | string(20) "i love php so much " 86 | 87 | */ 88 | 89 | ?> 90 | "; 108 | echo $age; 109 | 110 | ouptut: 111 | ------ 112 | Peter 113 | 43 114 | 115 | --------------------- 116 | other example 117 | 118 | parse_str("name=Peter&age=43",$myArray); 119 | echo "
    ";
    120 | print_r($myArray);
    121 | echo "
    "; 122 | 123 | output: 124 | ------ 125 | Array 126 | ( 127 | [name] => Peter 128 | [age] => 43 129 | ) 130 | 131 | 132 | String Functions - StrPos, StriPos, StrrPos 133 | 134 | 135 | strpos : sensitive to lettres 136 | stripos : insensitive // // 137 | strrpos : sensitive : it will search from the right but position from left 138 | 139 | 140 | $str= "i love php so much because php is funny"; 141 | $position = strpos($str,"php"); 142 | echo $position; 143 | 144 | output : 7 145 | String Functions - StrStr, StriStr, StrChr 146 | 147 | strstr /StrChr : sensitive 148 | strisrt : insensitive 149 | 150 | 151 | 152 | $str= "i love php so much because php is funny"; 153 | $char= strstr($str,"php",false); by default false 154 | echo $char; 155 | 156 | output: php so much because php is funny 157 | ------- 158 | 159 | -------------------------------- 160 | 161 | $str= "i love php so much because php is funny"; 162 | $char= strstr($str,"php",true); 163 | echo $char; 164 | output: php so much because php is funny 165 | ------- 166 | i love 167 | 168 | */ 169 | ?> 170 | if the 2 variable is the same ,the result will be 0 181 | =>if the variable ($string1) is greater than the over variable , the result will be positif and the sustraction of number between them 182 | as example : 3 183 | =>if the variable ($string2) is greater than the over variable , the result will be negatif and the sustraction of number between them 184 | will be -3 185 | -------------------- 186 | 187 | Strncmp(firstVariable,secondVariable,lenght) 188 | $string1="phpphp"; 189 | $string2="php"; 190 | echo $two=Strncmp($string1,$string2,3); 191 | from length 0 to 3 if the string is the same it will be 0 192 | 193 | --------------------- 194 | strrev: reverse the string 195 | 196 | $str= "moemen"; 197 | echo $reverse = strrev($str); 198 | 199 | output: nemeom 200 | String Functions - SubStr 201 | 202 | substr(name of string, length of start, length ) 203 | 204 | $str="i love php so much because its so beautifull"; 205 | echo $subs= substr($str,7); 206 | 207 | output: php so much because its so beautifull 208 | 209 | ------------------- 210 | substr 211 | 212 | $str="i love php so much because its so beautifull"; 213 | echo $subs= substr($str,7,18); the second parametre starts from offset 7 214 | 215 | output: php so much becaus 216 | 217 | 218 | 219 | String Functions - Substr_[Compare, Count] 220 | 221 | 222 | syntax : substr_count(name of string,"search",start,length) 223 | 224 | $str="i love php so much because php so funny"; 225 | 226 | echo $count= substr_count($str,"php",10,26); 227 | 228 | out : 1 229 | 230 | String Functions - substr_compare 231 | syntax : substr_compare(first string,second string,start from the first string ,lenght of word that i 232 | want to compare it,sensitivity :true or false); 233 | 234 | 235 | 236 | $string1="hello moemen"; 237 | $string2="moemen"; 238 | 239 | echo $string3= substr_compare($string1,$string2,6); 240 | 241 | output: 0 242 | 243 | */ 244 | ?> 245 | 251 | and we have another page "saadeh.php" and in this page we have 252 | if we want to echo this string from page to another page we must to include it like this 253 | 254 | 257 | 258 | include_once/require_once : include this same page 2 time its error and we use include_once ( if this page included , 259 | don't include it , if didnt include 260 | it before , include it) 261 | 262 | require / include the same 263 | but there a one difference between them : if the page doesnt exist include will give a warning and we can hide this warning by 264 | code in php , and the remainder code in the page will work 265 | 266 | but if we use require , it will give a error ! and remainder code willnot work 267 | 268 | 269 | Control Structure - Switch 270 | 271 | 272 | $browser = "google chrome"; 273 | switch($browser){ 274 | case"firefox": 275 | case"mozilla firefox": 276 | echo"your favorite browser is firefox"; 277 | break; 278 | 279 | case"chrome": 280 | case"google chrome": 281 | echo"your favorite browser is google chrome and this is the best"; 282 | break; 283 | 284 | case"safari": 285 | echo"your favorite browser is safari"; 286 | break; 287 | 288 | default: 289 | echo"your favorite browser isnt here"; 290 | } 291 | File System - File_Exists, Is_Writable/dirname(__FILE__) 292 | 293 | 294 | directory of file echo dirname(__FILE__); dirname(__FILE__)=__DIR__ 295 | output: C:\xampp\htdocs\learn\moemen 296 | 297 | 298 | 299 | 300 | $file= "love.txt"; 301 | if(file_exists($file)){ 302 | echo " the file [". $file ."] is found"; 303 | file_put_contents($file,"i love you so much"); 304 | } 305 | else{ 306 | echo " sorry the file [". $file ."] isnt found but i created it with php"; 307 | file_put_contents($file,"I LOVE YOU"); 308 | } 309 | 310 | !!file_exists : to search if the file found 311 | file_put_contents: to write information to file file name if isnt exist it will create a file with the same name of file 312 | that i didnt find it and write the information 313 | 314 | ----------------------------- 315 | 316 | $file= "love.txt"; 317 | if(is_writable($file)){ 318 | echo " the file [". $file ."] is found and it is writable"; 319 | } 320 | else{ 321 | echo " sorry the file [". $file ."] isnt found but it isnt writable"; 322 | file_put_contents($file,"I LOVE YOU"); 323 | } 324 | output: 325 | ------- 326 | sorry the file [love.txt] isnt found but it isnt writable 327 | Warning: file_put_contents(love.txt): failed to open stream: Permission denied in C:\xampp\htdocs\learn\moemen\el zero3.php on line 326 328 | !! in love.txt file properties we maked it read only 329 | 330 | 331 | File System - MkDir, RmDir, Is_Dir 332 | 333 | 334 | mkdir: will create a folder 335 | ------- 336 | $name="osama"; 337 | mkdir($name); 338 | 339 | 340 | 341 | rmdir: will remove a created folder 342 | ------- 343 | $name="osama"; 344 | rmdir($name); 345 | and jif we refresh the page other time we will get this warning 346 | Warning: rmdir(osama): No such file or directory in C:\xampp\htdocs\learn\moemen\el zero3.php on line 349 347 | 348 | ----------------------------- 349 | 350 | $name='love'; 351 | if(is_dir($name)){ 352 | rmdir($name); 353 | echo "the directory is removed succesufly"; 354 | }else{ 355 | echo "there is no directory with this name ". $name ; 356 | } 357 | */ 358 | 359 | ?> 360 | 459 | 460 | -------------------------------------------------------------------------------- /learnphp4.php: -------------------------------------------------------------------------------- 1 | "; 29 | print_r ($files); 30 | echo""; 31 | 32 | output: 33 | ------ 34 | Array 35 | ( 36 | [0] => . 37 | [1] => .. 38 | [2] => 122.txt 39 | [3] => 3554.txt 40 | [4] => aaa.txt 41 | [5] => love.bmp 42 | [6] => saadeh.txt 43 | [7] => zoro.rtf 44 | ) 45 | 46 | ----------------- 47 | 48 | $link= __DIR__."/php"; 49 | $files= scandir($link,SCANDIR_SORT_ASCENDING); 50 | echo"
    ";
     51 | print_r ($files);
     52 | echo"
    "; 53 | 54 | foreach($files as $file){ 55 | 56 | if(is_file($link."/".$file)){ 57 | unlink($link."/".$file); 58 | } 59 | } 60 | 61 | only folder we will stay because we used unlink = file only will remove 62 | output: 63 | ------ 64 | Array 65 | ( 66 | [0] => . 67 | [1] => .. 68 | [2] => ali 69 | [3] => moemen 70 | ) 71 | 72 | 73 | File System - Fopen 74 | 75 | fopen : handling to file 76 | fopen(filename,mode,include_path,context) 77 | 78 | mode: 79 | ----- 80 | r: read only start from the beginning of the file (file must be exist); 81 | r+: read and write start from the beginning of the file (file must be exist); 82 | w: write only (create the file if not exist); /// if there are contents in file it will remove and from starting it will start 83 | W+: write and read (create the file if not exist); /// if there are contents in file it will remove and from starting it will start 84 | a: write only (create the file if not exist);/// if there are contents in file it will leave it and 85 | write after them 86 | a+: write and read (create the file if not exist);///if there are contents in file it will leave it and 87 | write after them 88 | x: write only, (create the file if not exist);/// if the file is exist , it will give a warning or give an error 89 | Warning: fopen(moemen.txt): failed to open stream: File exists in C:\xampp\htdocs\learn\moemen\el zero4.php on line 91 90 | x+: write and read ,(create the file if not exist); /// if the file is exist , it will give a warning or give an error 91 | Warning: fopen(moemen.txt): failed to open stream: File exists in C:\xampp\htdocs\learn\moemen\el zero4.php on line 91 92 | 93 | 94 | File System - Fread 95 | fread(handle(file that i maked it with fopen),length) 96 | 97 | $filehandle= fopen("love.txt","r"); 98 | $filereading= fread($filehandle,filesize("love.txt")); 99 | echo $filereading; 100 | 101 | 102 | File System - Fwrite 103 | 104 | fwrite(handle,string(text that i want to write, length)) 105 | 106 | $filehandle= fopen("love.txt","r+"); 107 | $wite= fwrite($filehandle,"a"); 108 | 109 | os1ma will be as1ma 110 | 111 | File System - Fseek ,fclose 112 | 113 | fseek(handle ,offset,whence" by default SEEK_SET); 114 | whence : SEEK_SET ,SEEK_CUR ,SEEK_END : it start from end to beginning 115 | 116 | 117 | 118 | $filehandle= fopen("love.txt","r+"); 119 | fseek($filehandle,3,SEEK_SET); 120 | $write= fwrite($filehandle,"m"); 121 | fseek($filehandle,5,SEEK_CUR); // it starts from current length ,and we start with 1 ,isnt from 0 122 | $write= fwrite($filehandle,"a"); 123 | 124 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 125 | finally we must to close fopen with fclose fclose(filehandle) 126 | 127 | 128 | Predefined Variables - Globals 129 | 130 | call variable from anywhere 131 | 132 | $name ="moemen"; 133 | $GLOBALS['age'] ="saadeh"; 134 | function testFunc(){ 135 | 136 | echo $GLOBALS['name']; 137 | echo $GLOBALS['age']; 138 | } 139 | testFunc(); 140 | 141 | 142 | */ 143 | ?> 144 | 151 | 182 | 188 | 209 | 244 | 245 | 306 | 307 | output par example : 74H89DT54HS 308 | 309 | Filter - Filter_Var Advanced 310 | 311 | filter_var(variable,type of filter,option) 312 | 313 | 314 | Date - Date Intro 315 | 316 | date_default_timezone_set() 317 | 318 | date_default_timezone_set('Europe/Istanbul'); 319 | echo date('Y-m-d'); 320 | 321 | ------------------- 322 | 323 | Syntax 324 | date(format, timestamp) 325 | Format a local date and time and return the formatted date strings: 326 | example 327 | 328 | date_default_timezone_set('Europe/Istanbul'); 329 | $nextweek= time()+ (7*24*60*60); 330 | echo date('Y-m-d h:i:s' , $nextweek); 331 | 332 | 333 | */ 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | ?> 362 | 363 | -------------------------------------------------------------------------------- /oop.php: -------------------------------------------------------------------------------- 1 | OwnerName)<3){ 21 | echo 'Owner name can\'t be less then 3 chars'; 22 | }else{ 23 | echo 'Your name has been set'; 24 | } 25 | 26 | } */ 27 | 28 | public function setOwnerName($owner){ 29 | if(strlen($owner)ram ='2GB'; 41 | $iphone6plus->space ='32GB'; 42 | $iphone6plus->inch ='5 inch'; 43 | $iphone6plus->color ='gold'; 44 | $iphone6plus->setOwnerName('gren'); 45 | /* 46 | $iphone6plus->OwnerName ='ofs'; 47 | $iphone6plus->setOwnerName(); 48 | 49 | for running the second method 50 | -------------------------------- 51 | $iphone6plus->setOwnerName('moemen'); 52 | 53 | */ 54 | 55 | 56 | 57 | echo '
    ';
     58 |    var_dump($iphone6plus);
     59 |    echo '
    '; 60 | 61 | 62 | $iphone7plus = new AppleDevice(); 63 | 64 | $iphone7plus->ram ='4GB'; 65 | $iphone7plus->space ='124GB'; 66 | $iphone7plus->inch ='7 inch'; 67 | $iphone7plus->color ='red'; 68 | echo '
    ';
     69 |   var_dump($iphone7plus);
     70 |   echo '
    '; 71 | 72 | $iphone = new AppleDevice(); 73 | echo '
    ';
     74 |   var_dump($iphone);
     75 |   echo '
    '; 76 | 77 | 78 | 79 | /------------------------------------------------------------------ 80 | 81 | 82 | ram= $ra; 96 | $this->inch=$in; 97 | $this->space=$sp; 98 | $this->color=$co; 99 | } 100 | 101 | public function changeLock($lo){ 102 | $this->lock = sha1($lo) ; 103 | } 104 | } 105 | 106 | 107 | $iphone6plus = new AppleDevice(); 108 | $iphone6plus->ChangeSpec('4GB','7inch','32GB','gold'); 109 | $iphone6plus->changeLock('moemren123'); 110 | 111 | echo '
    ';
    112 |    var_dump($iphone6plus);
    113 |    echo '
    '; 114 | 115 | // echo $iphone6plus->lock; 116 | // Cannot access private property AppleDevice::$lock 117 | 118 | $iphone7plus = new AppleDevice(); 119 | $iphone7plus->ChangeSpec('16GB','37inch','64GB','white'); 120 | 121 | 122 | echo '
    ';
    123 |   var_dump($iphone7plus);
    124 |   echo '
    '; 125 | 126 | $iphone = new AppleDevice(); 127 | echo '
    ';
    128 |   var_dump($iphone);
    129 |   echo '
    '; 130 | 131 | /------------------------------------------------------------------ 132 | 133 | 134 | 135 | ram= $ra; 150 | $this->inch=$in; 151 | $this->space=$sp; 152 | $this->color=$co; 153 | } 154 | 155 | 156 | public function sayHello($n){ 157 | $this->name= $n; 158 | echo "Welcome to " . $n; 159 | } 160 | } 161 | 162 | 163 | 164 | class Sony extends AppleDevice{ 165 | public $screen = 'LCD'; 166 | public function sayHello($n){ 167 | $this->name= $n; 168 | echo "Welcome " . $n; 169 | } 170 | } 171 | 172 | 173 | $iphone6plus = new AppleDevice(); 174 | $iphone6plus->ChangeSpec('4GB','7inch','32GB','gold'); 175 | $iphone6plus->sayHello("IPhone"); 176 | $iphone6plus->price="300$"; 177 | 178 | echo '
    ';
    179 |    print_r($iphone6plus);
    180 |    echo '
    '; 181 | 182 | $sony = new Sony(); 183 | $sony->ChangeSpec('90GB','7inch','32GB','gold'); 184 | $sony->sayHello("Sony"); 185 | echo '
    ';
    186 |    print_r($sony);
    187 |    echo '
    '; 188 | 189 | 190 | /------------------------------------------------------------------ 191 | 192 | owner=$s; 210 | echo "Welcome " . $s; 211 | } 212 | 213 | } 214 | 215 | class Ipad extends MakeDevice { 216 | public $owner; 217 | public function TestPerfomance(){ 218 | echo "Performance is Good 100%"; 219 | } 220 | public function verifyOwner(){ 221 | echo "Owner is verified"; 222 | } 223 | public function sayWelcome($y){ 224 | $this->owner=$y; 225 | echo "Welcome " . $y; 226 | } 227 | } 228 | 229 | 230 | $iphone = new Iphone(); 231 | $iphone->sayWelcome("Moemen"); 232 | echo '
    ';
    233 |    print_r($iphone);
    234 |    echo '
    '; 235 | 236 | $ipad = new Ipad(); 237 | $ipad->sayWelcome("Ahmad"); 238 | echo '
    ';
    239 |    print_r($ipad);
    240 |    echo '
    '; 241 | 242 | 243 | 244 | ?> 245 | 246 | /------------------------------------------------------------------ 247 | 248 | getUsers(); 283 | $row->getArticles(); 284 | $row->showStats(); 285 | echo "
    ";
    286 |  print_r($row);
    287 |  echo "
    "; 288 | 289 | /------------------------------------------------------------------ 290 | 291 | 292 | name=$na; 299 | $this->ram=$ra; 300 | echo "Hello " . $na . "Your device has " . $ra ." Ram"; 301 | } 302 | } 303 | 304 | 305 | class Sony extends Iphone{ 306 | 307 | } 308 | 309 | $phone = new Iphone("Amani",4); 310 | 311 | 312 | ?> 313 | 314 | WelcomeToApp("Moemen","2GB"); 331 | 332 | 333 | ?> 334 | 335 | /------------------------------------------------------------------ 336 | 337 | ram = "4GB"; 357 | 358 | name= $n; 365 | $this->email= $e; 366 | } 367 | 368 | } 369 | 370 | 371 | $main= new Iphone("Moemen","moemensaadeh3@gmail.com"); 372 | $copy = clone $main; 373 | $main->name="ali"; 374 | $copy->name="khaled"; 375 | 376 | echo "
    ";
    377 |    print_r($main);
    378 |    echo "
    "; 379 | 380 | 381 | echo "
    ";
    382 |    print_r($copy);
    383 |    echo "
    "; 384 | 385 | ?> 386 | /------------------------------------------------------------------ 387 | 402 | 403 | 404 | 405 | pressPower()->bootPhone()->sayWelcome(); 427 | echo "
    ";
    428 |  print_r($phone);
    429 |  echo "
    "; 430 | 431 | ?> 432 | /------------------------------------------------------------------ 433 | fingerFeature()->threeD()->faceFeature()->sayHello(); 497 | echo "
    ";
    498 |  print_r($phone);
    499 |  echo "
    "; 500 | 501 | 502 | 503 | $sony = new Sony(); 504 | echo "
    ";
    505 |  print_r($sony);
    506 |  echo "
    "; 507 | $sony->faceFeature()->sayHello(); 508 | 509 | 510 | $nokia = new Nokia(); 511 | echo "
    ";
    512 |  print_r($nokia);
    513 |  echo "
    "; 514 | $nokia->sayHello(); 515 | ?> 516 | 517 | /------------------------------------------------------------------ 518 | 519 | sayHello(); 539 | echo "
    ";
    540 |  print_r($phone);
    541 |  echo "
    "; 542 | 543 | 544 | 545 | ?> 546 | 547 | /------------------------------------------------------------------ 548 | 549 | feature(); 572 | $phone->MoemenFeat(); 573 | echo "
    ";
    574 |  print_r($phone);
    575 |  echo "
    "; 576 | 577 | 578 | 579 | /------------------------------------------------------------------ 580 | ?> 581 | 582 | 583 | ?> 584 | 585 | 586 | 587 | ?> 588 | 589 | 590 | 591 | ?> 592 | --------------------------------------------------------------------------------