Hi..hello viewers!.. Welcome to "Parallel Code"

நமக்கு தேவையான அடிக்கடி பயன்படுத்தகூடிய variable-களை allvariable.php page-ல் declare செய்துகொள்ள வேண்டும். பிறகு அந்த variable-கள் எந்தெந்த page-களில் தேவை படுகிறதோ அந்த page-ல் முதல் line-ல் include 'allvariable.php' என்று கொடுத்து பிறகு உங்களுக்கு தேவையான variable-ஐ பயன்படுத்தி கொள்ளலாம்.

நீங்கள் ஒரு function-க்கு வெளியில் அந்த common variable-ஐ பயனபடுத்த விரும்பினால் நேரடியாக பயன்படுத்தி கொள்ளலாம். நீங்கள் ஒரு function-க்கு உள்ளே common variable-ஐ பயனபடுத்த விரும்பினால் gloabal $varable_name; என்று கொடுத்த பிறகு அந்த variable-ஐ பயன்படுத்தி கொள்ளலாம்.

Note: include 'allvariable.php'; என்ற இந்த line program கட்டாயம் இருக்கவேண்டும் மேலும் function-க்கு உள்ளே common variable-ஐ பயன்படுத்தும்போது global என்ற keyword-ஐ கட்டாயம் பயனபடுத்தி இருக்க வேண்டும்.

உங்களுக்கு புரியும் வகையில் ஒரு சிறிய example program கொடுக்கபட்டுளது.

allvariable.php

<?php
date_default_timezone_set('Asia/Kolkata');
$tdate = date('d-m-Y');
$ctime = date('h:i:s A');
$dbcon = mysqli_connect('localhost', 'root', '', 'test');
$width = "400px";
$height = "400px";
$wkdays = [
    'sunday',
    'monday',
    'tuesday'
];
?>

string_functions.php

<?php
include 'allvariables.php';

function testFunction() {
    global $width;
    global $tdate;
    global $ctime;
    global $dbcon;

    echo $width;
    echo $ctime;
    echo $tdate;
    
    global $wkdays;
    
    print_r($wkdays);
    
}


function createData(){
    global $dbcon;
    
    $q='insert into test_Table values()';
    $result=mysqli_query($dbcon,$q);
    
}

testFunction();
?>

web_details.php

<?php
include 'allvariables.php';

function selectData(){
    global $dbcon;
    global $width ;
    
    echo $width;
    $q='select * from student45';
    $result=mysqli_query($dbcon, $q);
    echo mysqli_error($dbcon);
}

selectData();
?>

Comments