PHP: Difference between revisions

From Alessandro's Wiki
No edit summary
No edit summary
 
(17 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== server ==
[[Category:Programming-Languages]]
* The eAccelerator and XCache PHP performance extensions are known to cause issues. If you're using either of those and are having problems, please disable them while you do your import. Add the following lines:
 
  php_value eaccelerator.enable 0
  php_value xcache.cacher off
  php_value xcache.optimizer off


== Language ==


=== Syntax ===


=== Syntax ===
==== if ====
==== IF ====
<syntaxhighlight lang="php" >
  if ( ( $A != "null" ) && (( $A === "true" ) || ( $A == "true" ) || ( $A <= 12 ) ) ) {
  if ( ( $A != "null" ) && (( $A === "true" ) || ( $A == "true" ) || ( $A <= 12 ) ) ) {
   /* code code code */
   /* code code code */
Line 17: Line 14:
   /* code code code */
   /* code code code */
  }
  }
==== FOR / foreach ====
</syntaxhighlight>
==== SWITCH ====
* abbreviation (as in C):
<syntaxhighlight lang="php" >
(something) ? (if true do this) : (else do this)
</syntaxhighlight>
==== for / foreach ====
==== switch ====
<syntaxhighlight lang="php" >
switch ($something) {
    case 0:
        echo "something is 0";
        break;
    case "hello":
        echo "something is $something";
        break;
}
</syntaxhighlight>
==== Strings ====
==== Strings ====
* joining strings:
<syntaxhighlight lang="php" >
$string1 . "string 2" . $_GLOBAL['string3'];
</syntaxhighlight >
==== Array ====
==== Array ====
* basic
<syntaxhighlight lang="php" >
Array("one", "two", "3" );
</syntaxhighlight>
* with index:
<syntaxhighlight lang="php" >
Array( 1 => "one", 2 => "two", 3 => "3" );
</syntaxhighlight >
* 2 dimensions
=== functions ===
* change file permissions
<syntaxhighlight lang="php" >
chmod($file,"0664");
</syntaxhighlight>
* sleep seconds
<syntaxhighlight lang="php" >
sleep( 2 ); //sleep 2 seconds
</syntaxhighlight>
* sleep nanoseconds
<syntaxhighlight lang="php" >
time_nanosleep(0, 200000000); //sleep 0.2 seconds
</syntaxhighlight>


=== My algorithms ===
=== My algorithms ===
Line 26: Line 66:
==== Cleaning strings for links ====
==== Cleaning strings for links ====
  Funzione per ripulire i titoli degli articoli da mettere nelle url
  Funzione per ripulire i titoli degli articoli da mettere nelle url
<syntaxhighlight lang="php" >
/* reference dictionary  */
   «  »  ò  ó  á  à  è  é  ù  ú  ì  í  ñ  ·  ç  :  .  ,  !  “  ”  '  '  €  &  '  @  %  $  ?
   «  »  ò  ó  á  à  è  é  ù  ú  ì  í  ñ  ·  ç  :  .  ,  !  “  ”  '  '  €  &  '  @  %  $  ?
  171,187,242,243,225,224,232,233,249,250,236,237,241,183,231,58,46,44,33,145,146,147,148,128,38,39,64,37,36,63         
  171,187,242,243,225,224,232,233,249,250,236,237,241,183,231,58,46,44,33,145,146,147,148,128,38,39,64,37,36,63         
Line 91: Line 133:
  return $urlizzato;
  return $urlizzato;
  }
  }
</syntaxhighlight>


=== Others algorithms ===
=== Others algorithms ===
Line 96: Line 139:
[http://www.pgregg.com/projects/php/preg_find/preg_find.php.txt preg_find.php]
[http://www.pgregg.com/projects/php/preg_find/preg_find.php.txt preg_find.php]
====simple glob() type replacement:====
====simple glob() type replacement:====
<syntaxhighlight lang="php" >
  $files = preg_find('/./', $dir);
  $files = preg_find('/./', $dir);
</syntaxhighlight>
====recursive?====
====recursive?====
<syntaxhighlight lang="php" >
  $files = preg_find('/./', $dir, PREG_FIND_RECURSIVE);
  $files = preg_find('/./', $dir, PREG_FIND_RECURSIVE);
</syntaxhighlight>
====pattern match? find all .php files:====
====pattern match? find all .php files:====
<syntaxhighlight lang="php" >
   $files = preg_find('/\.php$/D', $dir, PREG_FIND_RECURSIVE);   
   $files = preg_find('/\.php$/D', $dir, PREG_FIND_RECURSIVE);   
</syntaxhighlight>
====sorted alphabetically? ====
====sorted alphabetically? ====
<syntaxhighlight lang="php" >
   $files = preg_find('/\.php$/D', $dir, PREG_FIND_RECURSIVE|PREG_FIND_SORTKEYS);   
   $files = preg_find('/\.php$/D', $dir, PREG_FIND_RECURSIVE|PREG_FIND_SORTKEYS);   
</syntaxhighlight>
====sorted in by filesize, in descending order?====
====sorted in by filesize, in descending order?====
<syntaxhighlight lang="php" >
  $files = preg_find('/./', $dir,
  $files = preg_find('/./', $dir,
   PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC |PREG_FIND_SORTFILESIZE|PREG_FIND_SORTDESC);
   PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC |PREG_FIND_SORTFILESIZE|PREG_FIND_SORTDESC);
  $files=array_keys($files);
  $files=array_keys($files);
</syntaxhighlight>
====sorted by date modified?====
====sorted by date modified?====
<syntaxhighlight lang="php" >
  $files = preg_find('/./', $dir,
  $files = preg_find('/./', $dir,
   PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC |PREG_FIND_SORTMODIFIED);
   PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC |PREG_FIND_SORTMODIFIED);
  $files=array_keys($files);
  $files=array_keys($files);
</syntaxhighlight>
====parse command line argument to _GET array====
<syntaxhighlight lang="php" >
parse_str(implode('&', array_slice($argv, 1)), $_GET);
</syntaxhighlight>
== Server  ==
=== XCache ===
* The eAccelerator and XCache PHP performance extensions are known to cause issues. If you're using either of those and are having problems, please disable them while you do your import. Add the following lines:
* install '''(debina)'''
apt-get install php5-xcache
apt-get purge php-apc
vi /etc/php5/conf.d/xcache.ini
/etc/init.d/apache2 restart
<syntaxhighlight lang="php" >
  php_value eaccelerator.enable 0
  php_value xcache.cacher off
  php_value xcache.optimizer off
</syntaxhighlight>
* using xcache instead
<syntaxhighlight lang="php">
[xcache.admin]
xcache.admin.enable_auth = Off
; Configure this to use admin pages
xcache.admin.user = ""
; xcache.admin.pass = "md5staffofhashfashdffsdfdfsfd"
</syntaxhighlight>
<syntaxhighlight lang="php"  >
xcache.size  =                64M
xcache.var_size  =            64M
</syntaxhighlight>
<syntaxhighlight lang="php" >
xcache.cacher =              On
xcache.stat  =              On
xcache.optimizer =            On
xcache.coverager =          Off
</syntaxhighlight>
* admin gui :
cp -r /usr/sharexcache/admin/ /var/www/localhost/htdocs
http://domain/XCacheAdmin/
/etc/init.d/apache2 restart
=== various ===
* ''"A PHP debugging and profiling extension"''
xdebug

Latest revision as of 22:15, 10 February 2022


Language

Syntax

if

<syntaxhighlight lang="php" >

if ( ( $A != "null" ) && (( $A === "true" ) || ( $A == "true" ) || ( $A <= 12 ) ) ) {
  /* code code code */
}
else 
{
  /* code code code */
}

</syntaxhighlight>

  • abbreviation (as in C):

<syntaxhighlight lang="php" >

(something) ? (if true do this) : (else do this)

</syntaxhighlight>

for / foreach

switch

<syntaxhighlight lang="php" >

switch ($something) {
   case 0:
       echo "something is 0";
       break;
   case "hello":
       echo "something is $something";
       break;
}

</syntaxhighlight>

Strings

  • joining strings:

<syntaxhighlight lang="php" >

$string1 . "string 2" . $_GLOBAL['string3'];

</syntaxhighlight >

Array

  • basic

<syntaxhighlight lang="php" >

Array("one", "two", "3" );

</syntaxhighlight>

  • with index:

<syntaxhighlight lang="php" >

Array( 1 => "one", 2 => "two", 3 => "3" );

</syntaxhighlight >

  • 2 dimensions

functions

  • change file permissions

<syntaxhighlight lang="php" >

chmod($file,"0664");

</syntaxhighlight>

  • sleep seconds

<syntaxhighlight lang="php" >

sleep( 2 ); //sleep 2 seconds

</syntaxhighlight>

  • sleep nanoseconds

<syntaxhighlight lang="php" >

time_nanosleep(0, 200000000); //sleep 0.2 seconds

</syntaxhighlight>

My algorithms

Cleaning strings for links

Funzione per ripulire i titoli degli articoli da mettere nelle url

<syntaxhighlight lang="php" > /* reference dictionary */

 «   »   ò   ó   á   à   è   é   ù   ú   ì   í   ñ   ·   ç  :  .  ,  !   “   ”   '   '   €  &  '  @  %  $  ?
171,187,242,243,225,224,232,233,249,250,236,237,241,183,231,58,46,44,33,145,146,147,148,128,38,39,64,37,36,63         
function UrlizzaTit( $titoloUTF ) {
		//     Caratteri o stringhe da rimpiazzare col segno +
	$toPlus = array(
			chr(32),
			chr(145),
			chr(146),
			chr(37),
			chr(123),
			chr(125),
			chr(39),
			chr(64),
			chr(33),
			chr(36),
			chr(63),
			";",
			"[", 
			"]",
			"(",
			")",
			"\n",
			"-"
			);
	
		//     Caratteri o stringhe da cancellare    

	$toNull = array(
			"Il ",
			"Lo ",
			"lo ",
			"da ",
			" I ",
			" i ",
			" l' ",
			chr(147),
			chr(148),
			chr(171),
			chr(187),
			chr(58),
			chr(44),
			chr(46)
			);  

	$urlizzato1 = 	str_replace(chr(231),"c", 
			str_replace(chr(249),"u",
			str_replace(chr(250),"u",
			str_replace(chr(242),"o", 
			str_replace(chr(243),"o", 
			str_replace(chr(225),"a",
			str_replace(chr(224),"a",
			str_replace(chr(232),"e",
			str_replace(chr(233),"e",
			str_replace(chr(128),"euro+eur",
			str_replace(chr(236),"i",
			str_replace(chr(237),"i",
			str_replace(chr(241),"n",
			str_replace(chr(38),"and", $titoloUTF ))))))))))))));   

	$urlizzato2 = str_replace( $toNull, ""   , $urlizzato1 ); 
	$urlizzato  = str_replace( $toPlus, "+"  , $urlizzato2 );  

	return $urlizzato;
}

</syntaxhighlight>

Others algorithms

Qube @ Efnet preg_find.php

simple glob() type replacement:

<syntaxhighlight lang="php" >

$files = preg_find('/./', $dir);

</syntaxhighlight>

recursive?

<syntaxhighlight lang="php" >

$files = preg_find('/./', $dir, PREG_FIND_RECURSIVE);

</syntaxhighlight>

pattern match? find all .php files:

<syntaxhighlight lang="php" >

 $files = preg_find('/\.php$/D', $dir, PREG_FIND_RECURSIVE);  

</syntaxhighlight>

sorted alphabetically?

<syntaxhighlight lang="php" >

 $files = preg_find('/\.php$/D', $dir, PREG_FIND_RECURSIVE|PREG_FIND_SORTKEYS);   

</syntaxhighlight>

sorted in by filesize, in descending order?

<syntaxhighlight lang="php" >

$files = preg_find('/./', $dir,
 PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC |PREG_FIND_SORTFILESIZE|PREG_FIND_SORTDESC);
$files=array_keys($files);

</syntaxhighlight>

sorted by date modified?

<syntaxhighlight lang="php" >

$files = preg_find('/./', $dir,
 PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC |PREG_FIND_SORTMODIFIED);
$files=array_keys($files);

</syntaxhighlight>

parse command line argument to _GET array

<syntaxhighlight lang="php" > parse_str(implode('&', array_slice($argv, 1)), $_GET); </syntaxhighlight>

Server

XCache

  • The eAccelerator and XCache PHP performance extensions are known to cause issues. If you're using either of those and are having problems, please disable them while you do your import. Add the following lines:
  • install (debina)
apt-get install php5-xcache
apt-get purge php-apc
vi /etc/php5/conf.d/xcache.ini
/etc/init.d/apache2 restart

<syntaxhighlight lang="php" >

 php_value eaccelerator.enable 0
 php_value xcache.cacher off
 php_value xcache.optimizer off

</syntaxhighlight>

  • using xcache instead

<syntaxhighlight lang="php"> [xcache.admin] xcache.admin.enable_auth = Off

Configure this to use admin pages

xcache.admin.user = ""

xcache.admin.pass = "md5staffofhashfashdffsdfdfsfd"

</syntaxhighlight>

<syntaxhighlight lang="php" > xcache.size = 64M xcache.var_size = 64M </syntaxhighlight>

<syntaxhighlight lang="php" > xcache.cacher = On xcache.stat = On xcache.optimizer = On xcache.coverager = Off </syntaxhighlight>

  • admin gui :
cp -r /usr/sharexcache/admin/ /var/www/localhost/htdocs
http://domain/XCacheAdmin/
/etc/init.d/apache2 restart

various

  • "A PHP debugging and profiling extension"
xdebug