Using the variable outside of function()

Hello,

Using the variable outside of function()
PHP codes are at the top, I want to get the printout in the body
I can output in function. No problem.
However, I want to print inside the body
It is possible to do the data in session, but is there any other way?

<?php

for ($vi = 0; $vi < count($hedefsecenek); $vi++) {
    $hsecs = explode("-", $hedefsecenek[$vi]);
    $hsecsay = count($hsecs);
    
    
    if ($hsecsay == 1){ 
    $vartekli = array_search($hsecs[0], $sonluarray);
    if ($vartekli !== false) {
    teklivarsagoster( 
     $bulunan_toplam_cikis=$hsecs[0],
     $bulunan_marka=$marka, $daire_sayisi,
     $bulunan_sonlu_id=$sids[$vartekli]
     );
    break 2;
    }
    }  // if ($hsecsay == 1){
    //
    // There are 4900 more lines of code here
    //
    } // for
    
function teklivarsagoster($bulunan_toplam_cikis, $bulunan_marka, $daire_sayisi, $bulunan_sonlu_id) {

$sql = "SELECT * FROM table";

        while ($row = $sql->fetch()){
$table_row = "";
$table_row .= "";
$table_row .= "";
$table_row .= "";
$table_row .= "";
}


} // function teklivarsagoster
?>
<!DOCTYPE>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body>
  <?php
  
  echo $table_row;
  
   ?>
  </body>
</html>

Functions should return the result they produce to the calling code.

I think I could not tell, or i didn’t understand

At the top the function is already calling

    teklivarsagoster( 
     $bulunan_toplam_cikis=$hsecs[0],
     $bulunan_marka=$marka, $daire_sayisi,
     $bulunan_sonlu_id=$sids[$vartekli]
     );

I can print with echo in this

function teklivarsagoster($bulunan_toplam_cikis, $bulunan_marka, $daire_sayisi, $bulunan_sonlu_id) {

However, I want to print in the body
According to the algorithm in the top function, the result is formed in the following function.

A couple options:

Using return:
function increment ($param) {
$result = $param++;
return $result;
}

Or pass by reference:
function increment (&$param) {
$param++;
}

I do not have enough information
I need to get echo fields
How should I do it?

function dvrvarsagoster($bulunan_toplam_cikis, $bulunan_marka, $kamera_sayisi, $bulunan_dvr_idler) {
      $bulunandvr_idler = join(',',$bulunan_dvr_idler);
      $dvridlervesayilari = array_count_values($bulunan_dvr_idler);
      $rowspan = count($dvridlervesayilari);

      echo "
      <table class=\"table table-bordered table-hover\">
          <thead>
          <tr class=\"bg-primary\">
            <th class=\"col-md-0\">Seç</th>
            <th class=\"col-md-2\">DVR Markası</th>
            <th class=\"col-md-2\">Ses Girişi</th>
            <th class=\"col-md-3\">Kmr Girişi</th>
            <th class=\"col-md-3\">DVR Adı & Tanımı</th>
            <th class=\"col-md-2\">Fiyatı</th>
            <th class=\"col-md-0\">Adet</th>
          </tr>
          </thead>
          <tbody>";
          
          echo "
          <tr>
              <td rowspan=\"".$rowspan."\" style=\"vertical-align: middle;\"><input type=\"radio\" class=\"dvr_getir\" value=\"".$bulunandvr_idler."\" name=\"secilen_dvr\" /></td>";


      global $PDO;

          $arr = array_map('trim', explode(',',$bulunandvr_idler));
          $in  = str_repeat('?,', count($arr) - 1) . '?';

          $sql = "
          SELECT 
          dvrlar.id,
          dvrlar.dvr_markasi, 
          dvrlar.dvr_audioin,
          dvrlar.dvr_camerain,
          dvrlar.dvr_adi,
          dvrlar.birim_fiyati,
          dvrlar.dvr_para_birimi,
          dvrlar.stok, 
          markalar.urun_markasi,
          dvrtipi.dvr_audio_in,
          parabirimi.para_birimi,
          dvrtipi.dvr_camera_in  
          FROM dvrlar
          INNER JOIN parabirimi ON dvrlar.dvr_para_birimi = parabirimi.id 
          INNER JOIN markalar ON dvrlar.dvr_markasi = markalar.id
          INNER JOIN dvrtipi ON dvrlar.dvr_audioin = dvrtipi.id          
          WHERE dvrlar.id IN ($in)";

          $dvr_ara = $PDO->prepare($sql);
          $params = array_merge($arr);
          $dvr_ara->execute($params);

          $dvr_sayisi =  $dvr_ara->rowCount();


      $i=0;
      $urun_adet = 0;        
      while ($row = $dvr_ara->fetch()){

        global $cf;
        global $secilen_para_birimi;
        $urun = array("b_fiyati"=>$row['birim_fiyati'], "para_birimi"=>$row['para_birimi']);
        $fiyati = convert($cf,$urun,$secilen_para_birimi);
        $fiyati_d = para($fiyati);

        if($i == 0){
        echo "
            <td><a style=\"cursor:pointer;\" title=\"Ürün Açıklaması\" onClick=\"aciklama('dvrlar', '" . $row['id'] . "')\"><span class=\"glyphicon glyphicon-info-sign\"></span></a> ".$row['urun_markasi']."</td>
            <td>".$row['dvr_audio_in']."</td>
            <td>".$row['dvr_camera_in']."</td>
            <td>".$row['dvr_adi']."</td>
            <td>".$fiyati_d." ".$secilen_para_birimi."</td>
            <td>".$dvridlervesayilari[$row['id']]."</td>
          </tr>";
        }else{

        echo "
        \n<tr>
          <td><a style=\"cursor:pointer;\" title=\"Ürün Açıklaması\" onClick=\"aciklama('dvrlar', '" . $row['id'] . "')\"><span class=\"glyphicon glyphicon-info-sign\"></span></a> ".$row['urun_markasi']."</td>
          <td>".$row['dvr_audio_in']."</td>
          <td>".$row['dvr_camera_in']."</td>
          <td>".$row['dvr_adi']."</td>
          <td>".$fiyati_d." ".$secilen_para_birimi."</td>
          <td>".$dvridlervesayilari[$row['id']]."</td>
        </tr>";
        }


     $urun_adet += $dvridlervesayilari[$row['id']];
     $i++;
    }
    
    echo "
    \n<tr class=\"alert alert-info\">
      <td colspan=\"7\">".$kamera_sayisi." kamera için <b>".strtoupper($bulunan_marka)."</b> markadan ".$urun_adet." adet ".$bulunan_toplam_cikis." kamera girişli DVR elde edilmiştir</td>
    </tr>";

      echo "\n</tbody>\n</table>";

}

The code you are putting inside of the function is part of your application code. It does not belong inside a function. All this is doing is creating more work for you when writing the code and making it harder to get your code to work. There’s no value being added by doing this. Functions should be general-purpose, simple building blocks, and only do one thing each.

For a dynamically built web page, your html document should be treated as a template, using simple php code inside of it, or you should use an actual template system. If the section of output you are showing is a repeated region, just get all the data needed into an appropriately named php array variable, then test/loop over this data at the correct point in the html document.

In general. the code for any web page should be laid out like this -

  1. initialization - define, require, create, … things your page needs, such as the session_start() statement, a database connection, configuration values, …
  2. post method form processing code - a post method form should be used for things that create/update data on the server or perform an action such as sending an email.
  3. get method business logic - get/create data needed to display the dynamic content on the web page.
  4. html document/template - using simple php code or an actual template system, produce the actual html document, using the data produced from the above sections of code.

Any sql query(ies) to get data needed to display the page should be located in item #3 in the above list.

The working logic of this page is as follows
1.How many brands are there for this product first
and brands are creating array

  1. There is a foreach for brand arrays

First looking for the ID of the product with FINITE feature
then searches for IDs of CASCADE featured product.
While doing this, it checks stock status
Searches for the product according to the desired inputs and outputs
It’s just like that, a little more complicated

If I exclude this variable from the function, only an id comes
$finite_id_found
if this variable stays in the function all id comes

Sample has 4 brands

More outputs

Sponsor our Newsletter | Privacy Policy | Terms of Service