PHP - Doing Define Wrong
im doing it wrong can some one help me ? , thanks
{ Code: [Select] define('userlevel'( 9="admin" , 1="guest"));} Similar TutorialsHi all, I was reading a bit in the manual about the defined function. Since i have seen it being used quite a lot to disallow direct access. But one of the comments made me think if there are any alternatives to get the same. it's this comment: http://www.php.net/manual/en/function.defined.php#89886 If i read it correct it tells that defined() seems to be pretty slow. I was thinking, if that is the case what are the alternatives... Does anyone know how to interpret this comment and if there are alternatives? I am not yet a guru tofully understand the results. cheers! In many cases, variable have to be explicitly defined for whatever reason. However, often it is not required for a simple web page script. Yet, even in those cases, if a variable is NOT explicitly defined, the web server error log (example: my_domain.com-error_log) will rack up a lot of entries such as this: Quote [Fri Dec 31 01:31:43 2010] [error] [client 123.123.123.123] PHP Notice: Undefined variable: some_variable_name in /home/my_domain/public_html/some_page.php on line 6 A busy site could result in large error logs... What is considered "best practice"? Should all variables always be explicitly defined with the var key word? I have at the very top of my web page the following. Not inside any function in case that would have a scope issue. Define ('GROUP', '1'); Define ('ROUNDOF16', '2'); Define ('QUARTERFINAL', '3'); Define ('SEMIFINAL', '4'); Define ('FINAL', '5'); Lower down I have if($Round == 5) This works but this if($Round == FINAL) has error Parse error: syntax error, unexpected T_FINAL in ....... I thought this was correct ! Imagine a site programmed using full OOP. Where would you store information like this: $companyName = "ABC Ltd"; $companyPhone = "02476 999 999"; $companyAddress etc... Would you just define them in the public scope? (and use global or pass them in when needed??) Is there a better way? They will be needed in more places than just a navigation bar and would be needed by more than one class. i tried googling but didnt really know how to "google it" as i cant think of a short phrase that best describes it.. but i want to chance the database setting of "localhost" in my php script to another websites database how do i acheive this? Ive got a simple function that's counting percentages of the results, and what I want is when the first line does $variable / 100 - to go on 2 decimals(ex. 0.72142141 what I want is to write 0.72). Code: [Select] function postotak(){ $p = $bodovi / 100; $postotak = $p * 100; im having a problem getting this code to work...i know it may not make sence why i would do this but it is for a larger script...is there any reason why it wont echo please? define ('TEST_VAR', 'hello'); $var = "TEST"; echo {$var}_VAR; Hi, I have the define(); function where i need to put the variable for directory name which is relative ... How to do that ... Code: [Select] <?php if (TARGET_PLATFORM == FILE_SYSTEM_PLATFORM) { /** * if FILE_SYSTEM_PLATFORM is selected as TARGET_PLATFORM, * then assign root folder adress to FILE_ROOT macro */ define("FILE_ROOT", "$diro"); } public function __construct($fileRoot) { if (is_dir($fileRoot)) { $this->fileRoot = $fileRoot; //if ($this->fileRoot[strlen($this->fileRoot) - 1] != '/' ) { // $this->fileRoot .= '/'; //} } else { die($fileRoot .' is not a directory '); } } ?> When i go with this, it says this: " 'dir name ' is not a directory". But I need the variable for this to work as I want to. Does anyone have an idea? To make it clear, this is only the small part of code, which is a part of the directory root for the directory tree. I am trying to use absolute path across my project excluding the domain as of now and I am trying to do it as follows: Code: [Select] define('root', '/php_projects/myproject/'); // The Standard Data $path = "path.php"; $connectvars = "connectvars.php"; // The CSS Styles $reset_css = root . "view/reset.css"; $style_css = root . "view/style.css"; This works so far, the only problem I am having is that it is producing a notice. And my question is, how can I get rid of the notice? The notice is this one: Code: [Select] Notice: Constant root already defined in C:\xampp\htdocs\php_projects\myproject\path.php on line 3 Why does the notice get produced in first place? I never defined it twice, as it states. How come I cannot do this? define('IMG_EXT', array("jpg", "jpeg", "png", "gif", "bmp", "tif"), true); <?php if (isset($_SESSION['first_name'])) { echo "<div id=\"login_status\">Welcome, Daniel | <a href=\"BASE_URL sm-login/panel\">My Account</a> | <a href=\"BASE_URL sm-login/logout\">Logout</a></div><!-- Show If Logged In -->"; } ?> this is my code but BASE_URL is a define variable containing = http://localhost/ when i run the script php treats it as just BASE_URL e.g. BASE_URL%20sm-login/pane.php not http://localhost/sm-login/panel.php i know the define is working cos i echo it out earlyer My goal was to create a constant that could be included on the pages, that no matter where it was located in the structure, would be able to find the template files, css, images, etc. using the following code: Code: [Select] if($_SERVER['HTTP_HOST'] == "localhost"){ define('SITEURL', 'http://' . $_SERVER['HTTP_HOST']); define('SITEPATH', $_SERVER['DOCUMENT_ROOT']); define('CSS', $_SERVER['DOCUMENT_ROOT'] . '/css/'); define('IMAGES', $_SERVER['DOCUMENT_ROOT'] . '/images/'); } else{ define('SITEURL', 'http://' . $_SERVER['HTTP_HOST']); define('SITEPATH', $_SERVER['DOCUMENT_ROOT']); define('TEMPLATE', $_SERVER['DOCUMENT_ROOT'] . '/incs/template/'); define('CSS', $_SERVER['DOCUMENT_ROOT'] . '/css/'); define('IMAGES', $_SERVER['DOCUMENT_ROOT'] . '/images/'); } I put the above in a variables.php file that is in the includes folder (named incs) and I call it using the typical include statement. The problem is that when I use the constant to grab the CSS file, its not working when using: Code: [Select] <link rel="stylesheet" href="<?php echo CSS . "template.css" ?>" type="text/css" media="screen" /> I can view the page source and it has the path right, but I cannot figure out how to get it to actually pull in the css file. I know the echo command is not correct, at least I dont think it is, but have not been able to figure out any other way. Also, is there a better way to define the constants to the file locations and have it automatically detect whether its on the test server or production and then be able to reference those locations no matter where it is in the structure? I would like to know how to use an underscore in a define function like this example below. Code: [Select] /** The name of the database for WordPress */ define('DB_NAME', 'Database_name'); /** MySQL database username */ define('DB_USER', 'User_name'); /** MySQL database password */ define('DB_PASSWORD', 'password_here'); /** MySQL hostname */ define('DB_HOST', 'Dtabase_host'); Any help appreciated and thanks in advance. <HTML> <?php $arr = array("sue" => "betsy", "frank" => "marge"); ?> </HTML> This produces the following HTML page: "betsy", "frank" => "marge"); Its like when I use the => to make my associative array, the code thinks I'm closing the php codeblock and then just prints the rest of it to the screen... Hi , how I can save something into form? I mean , insert something in variable and than insert it into value in the form so when i send the form the value will be send too. I also want that it will be can not change. for example : <input name="email" value="a@s.d"/> but this example can be change if you insert something else. thanks , Mor. I'm being told I have an undefined variable. Similar to sumUsagekWH (in the code below) there are other placeholders called sumUsageKHW, sumUsageWKWp, sumUsageWKWi and so on. Each script of code looks similar to below where the 'totalUsage' is added into sumUsageKHW, sumUsageKHE and so on. All references are highlighted red below to make it easier to follow.
So, if I am only using these sumUsagesXXXX's as placeholders for the following post....
<input name="electric_charge_total" type="hidden" id="electric_charge_total" value="<?php echo number_format($basic_charge + ($sumUsageKWH * $energy_charge) + ($sumUsageWKWp * $peak_rate) + ($sumUsageWKWi * $intermediate_rate) + ( ($sumUsageWKW+$sumUsageWEW) * $base_rate),"2"); ?>" align="right"/>
how to I define them in PHP?
Based on the code below, they are only used as a way to introduce totalUsage ( it looks silly the way it was done, but later the code will be expanded so it makes sense). What I mean is that totalUsage is being entered (in the below case) into sumUsageKWH from the $sumUsageKWH += $row[totalUsage]
Notice: Undefined variable: sumUsageKWH in C:\xampp1\htdocs\Utrack\invoice.php on line 73
<?php $sql = ";WITH TOTAL_KWH_WINTER AS ( SELECT CONVERT(VARCHAR(10),cdate,111)AS trans_date, datepart(hh, cdate) as trans_hour, comm_id, MIN((total_energy_a+total_energy_b+total_energy_c)/100) AS minUsage, MAX((total_energy_a+total_energy_b+total_energy_c)/100) AS maxUsage, meter_multiplier FROM [radiogates].[dbo].[purge_data] LEFT OUTER JOIN [radiogates].[dbo].[ops_invoice] on [radiogates].[dbo].[purge_data].[comm_id] = [radiogates].[dbo].[ops_invoice].[meter_id] where comm_id='$comm_id'and meter_multiplier is not null group by comm_id, CONVERT(VARCHAR(10),cdate,111), datepart(hh, cdate), meter_multiplier ) SELECT *, datepart(weekday, trans_date) as trans_date_day, datepart(month, trans_date) as trans_date_month, ((maxUsage - minUsage)*meter_multiplier) as totalUsage FROM TOTAL_KWH_WINTER where datepart(weekday, trans_date) IN ('1', '2', '3', '4', '5', '6', '7') AND DATEPART(MONTH, trans_date) IN ('10','11','12','1','2','3','4') and trans_date BETWEEN '$startdate2 00:00:01' AND '$enddate2 24:00:00' "; $query = sqlsrv_query($conn, $sql); if ($query === false){ exit("<pre>".print_r(sqlsrv_errors(), true));}while ($row = sqlsrv_fetch_array($query)) { $sumUsageKWH += $row[totalUsage];}sqlsrv_free_stmt($query);?> As you can see here I have the following code Code: [Select] <?php session_start(); session_register('hazard2'); //INITIALIZE ERROR VARIABLES $errorFound = false; $errorIcon['hazard2'] = ''; if (isset($_POST['submitbutton'])) { //SET SESSION VARIABLES $_SESSION['hazard2'] = $_POST['hazard2']; //TEST FORM INFORMATION if(count($_POST['hazard2']) != 3) { $errorFound = true; $errorIcon['hazard2'] = 'Error Question 2'; } //IF NO ERRORS WERE FOUND, CONTINUE PROCESSING if (!$errorFound) { header( "Location: hazardresult.php" ); } } ?> It's lazy I know, but the script is on a file called hazard2.php, is there anyway to have all of the hazard2's in the code come from the file name. So when I make hazard3, I don't have to change all the hazard2's to hazard3's Thank you I have a dummy pagination script in place on my website that allows for links like index.php?id=whatever. This script some how makes my title defintion function not work. I dont get any errors, but it still wont show the correct title. Heres what I have: header.php (look inside title tag) <?php $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; $start = $time ?> <?php function ShowMenu() { echo'<div id="left"> <div class="navouter"> <div class="orange"></div> <div class="navheader">Main Menu</div> <div class="navcontent"> <ul> <li><a href="/index.php">Home</a></li> </ul> Stuff <ul> <li><a href="/index.php?id=about">About</a></li> <li><a href="/index.php?id=projects">Projects</a></li> <li><a href="/index.php?id=contact">Contact</a></li> <li><a href="http://paste.neverpool.com">Pastebin</a></li> </ul> </div> </div> <div class="navouter"> <div class="orange"></div> <div class="navheader">Friends</div> <div class="navcontent"> <ul> <li><a href="http://exile-studios.com" target="_BLANK">Exile Studios</a></li> </ul> </div> </div> </div>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <?php include("/home/-/public_html/timer.txt"); ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php if (defined('TITLE')) { print TITLE; } else { print 'NeverPool'; } ?></title> <link rel="stylesheet" type="text/css" href="/style.css" /> </head> <body onload="countdown(year,month,day,hour,minute)"> index.php <?php include('/home/-/public_html/header.php'); ?> <?php $id = $_GET['id']; if ($id == 'about') { include '/home/-/public_html/about.php'; } else if ($id == 'projects') { include '/home/-/public_html/projects.php'; } else if ($id =='contact') { include '/home/-/public_html/contact.php'; } else { ?> <div id="outer"> <div id="right"> <h1>The Elder Scrolls V: Skyrim</h1><div class="htimeauth"> 12/14/10 | <a href="/contact.php">Creech</a></div> <p>The newest installment of The Elder Scrolls series has been announced by Bethesda and I have to say, I'm VERY excited.. I've been following The Elder Scrolls series since Daggerfall and I have to have this game. It's due to come out 11/11/11. I am so going to preorder this game.</p> <h1>Hello, world!</h1><div class="htimeauth"> 12/7/10 | <a href="/contact.php">Creech</a></div> <p>Hullo! I went ahead and threw up the new template for the site. Hope you enjoy it! Also, while your here, check out my <a href="/projects.php">projects</a> page for updates on my pastebin and other such projects. </p> </div> <?php } ShowMenu(); echo('</div>'); include('/home/-/public_html/footer.php'); ?> projects.php (sample) <?php define('TITLE', 'Projects'); ?><div id="outer"> <div id="right"> <h1>Pastebin Updates - PHP</h1><div class="htimeauth"> 12/10/10 | <a href="/contact.php">Creech</a></div> <p>My most recent project has been my newest pastebin. It uses the <a href="http://www.zlib.net/" target="_BLANK">zLib Library</a> to compress the created pastes. It features an indexing system that allows you to view recent posts made by other people, and it supports PHP syntax highlighting. It does not require a MySQL database of any kind, and as far as I know, it's completely secure. <del>I'm still working on the template for it</del>, but almost all of the PHP programming part of it is finished. </p> <p>Added 12/13/10: Added a link to the pastebin on my side menu. Here's another <a href="http://paste.neverpool.com">Link</a>. The template I said I was working on is not going to happen, I find it easier to just leave it white.</p> </div> Also, here is a live demonstration: http://neverpool.com Any help solving this problem would be very appreciated. I have a main file index.php into which iam loading myinnerpage.php. I have defined a variable inside index.php & checks it on myinnerpage.php, but when i load innerpage it shows restricted access, any idea why? following is my code. index.php Code: [Select] <?php define( '_JEXEC', true ); ?> <div class="mypage"> </div> <ul> <li><a href="#" onclick="loadpages('myiinerpage.php')">about page</a></li> </ul> <script type="text/javascript"> var $jq = jQuery.noConflict(); function loadpages(page) { $jq('.mypage').load('myfolder/'+page); } </script> and now in the myiinerpage.php which is in the folder myfolder i have the following code. Code: [Select] <?php defined('_JEXEC') or die('Restricted access'); ?> <div> my page elements </div> but when i click on the link it shows restricted access. I have loaded jquery too, any idea y this is happening? Is this proper PHP... function setHeat(Building $b) { $this->hotness = $b->getInsideHeat(); } That looks more like Java to me. I thought you didn't define data-types anywhere in PHP?! TomTees |