Here, for example, the code:

if ( preg_match("/^все/", $pere) ) { $q = "SELECT `opisanie` FROM `daba` where table='все' limit 0,2"; $r = mysql_query("$q;") or die("Что-то не так."); while ($r = mysql_fetch_object($re)) print_r($r->opisanie); } if ( $a == $b ) { $q = "SELECT `opisanie` FROM `daba` where table='все' limit 0,2"; $r = mysql_query("$q;") or die("Что-то не так."); while ($r = mysql_fetch_object($re)) print_r($r->opisanie); } 

Option:

 if ( preg_math("/^все/, $pere) && $a == $b ) 

And option:

 if ( ( preg_match("/^все/", $pere) ) || ( $a == $b ) ) { $sql = "SELECT `opisanie` FROM `daba` where `table`='все' limit 0,2;"; $res = mysql_query($sql) or die("Что-то не так."); while ( $row = mysql_fetch_object($res) ) echo $row->opisanie; } 

Does not fit.

How to avoid repeating code execution?

UPD : the problem is settled, redid the code ...

  • one
    <code> <pre> if (preg_match ("/ ^ all /", $ pere)) show (); if ($ a == $ b) show (); function show () {$ sql = "SELECT opisanie FROM daba where table = 'all' limit 0,2;"; $ res = mysql_query ($ sql) or die ("Something is wrong."); while ($ row = mysql_fetch_object ($ res)) echo $ row-> opisanie; } </ code> </ pre> - ReinRaus

2 answers 2

Something like this:

 if ( ( preg_match("/^все/", $pere) ) || ( $a == $b ) ) { $sql = "SELECT `opisanie` FROM `daba` where `table`='все' limit 0,2;"; $res = mysql_query($sql) or die("Что-то не так."); while ( $row = mysql_fetch_object($res) ) echo $row->opisanie; } 
  • this option is also not suitable ... Just where it is checked with preg_match, there is also code that does not need to be executed when $ a == $ b. - nick777
  • one
    So everything is correct, just in the {} block, make if ((preg_match ("/ ^ all /", $ pere)) || ($ a == $ b)) {/ * common / if ($ a == $ b) {/ only for $ a == $ b /}} You can still make conditions: $ con1 = (bool) preg_match ("/ ^ all /", $ pere); $ con2 = ($ a == $ b); $ con3 = true; / some other condition / if ($ con1 || $ con2) {/ total / if ($ con1) {/ for preg_match /} elseif ($ con2) {/ for $ a == $ b /} else {/ for $ con3 * /}} - Sh4dow
  • @ nick777, So write all the code you need to organize! And we will guess for eternity that you need something, and even more than one ... - timka_s

Did it ever occur to you to just take out the repeated piece of code into a separate function?