PHP return - beginner fault - php

I do not know what I'm doing wrong. Perhaps someone could advise me. I'm trying to define seasons and then use the result in index.
...
File with function:
function getCurrentTheme($for_area) {
// Definition of seasons
$spring='spring theme title';
$spring_season=array('03-21',......);
$summer='summer theme title';
$summer_season=array(......);
$autumn='autumn theme title';
$autumn_season=array(......);
$winter='winter theme title';
$winter_season=array(......);
// Today
$current_date=date('m-d');
// So what season is now?
if ($for_area==='some_area') {
if (in_array($current_date,$spring_season) {
$theme=$spring;
}
else if (in_array($current_date,$summer_season) {
$theme=$summer;
}
else if (in_array($current_date,$autumn_season) {
$theme=$autumn;
}
else if (in_array($current_date,$winter_season) {
$theme=$winter;
}
else {}
}
else if ($for_area==='other_area') {
// ...
}
else {}
return $theme;
}
...
Index:
$area='some_area';
getCurrentTheme($area);
// And here is the fault. Hope sometimes I will stop being retarded.
echo $theme;
// What should be printed?
summer theme title
Thanks in advance and please try understand my innocence.

You need to store the value, so that you can then echo it:
$area='some_area';
$theme = getCurrentTheme($area);
echo $theme;

You're not storing the value returned from the getCurrentTheme function. Either store it in a variable and then echo it
$theme = getCurrentTheme($area);
echo $theme;
or simply echo the call without having to use a variable.
echo getCurrentTheme($area);
Your choice!

Related

How to check if constant is defined, inside function - php

I am trying to make a small PHP function which can check if a constant is defined, and if so, echo it, and if not, echo space or nothing.
Right now, the if(defined() part is not working, because the constant is being transferred to a variable inside the function.
function getConstant($constant) {
if(defined($constant)) {
echo constant($constant);
} else {
echo '';
}
}
The echo constant($constant) part is working fine, but I cannot check if the constant is actually defined because it is a variable now.
I cannot seem to find a solution for it
public static function isConstants($constant) {
$oClass = new ReflectionClass(__CLASS__);
$allConstants = $oClass->getConstants();
if (isset($allConstants[$constant])) {
echo $allConstants[$constant];
} else {
echo '';
}
}

How to call php function with URL?

Hello Id like to know how to call the function I just written from URL?
Bellow are my php code.
<?php
require 'db.php';
function l1(){
echo "Hello there!";
}
function l2(){
echo "I have no Idea what Im doing!";
}
function l3(){
echo "I'm just a year 1 college student dont torture me sir!";
}
?>
I tried http://127.0.0.1/study/sf.php?function=l1 but it wont echo the written code.
Please point me to the right direction.
Yes you could supply that parameter into calling your user defined function:
$func = $_GET['function'];
$func();
Might as well filter the ones you have defined thru get_defined_functions
function l1(){
echo "Hello there!";
}
function l2(){
echo "I have no Idea what Im doing!";
}
function l3(){
echo "I'm just a year 1 college student dont torture me sir!";
}
$functions = $arr = get_defined_functions()['user']; // defined functions
if(!empty($_GET['function']) && in_array($_GET['function'], $functions)) {
$func = $_GET['function'];
$func();
}
Sample Output
Sidenote: function_exists can be also applied as well:
if(!empty($_GET['function']) && function_exists($_GET['function'])) {
// invoke function
}
One option you can do if use if/elseifs like so:
if($_GET['function'] == 'l1')
{
l1();
}
else if($_GET['function'] == 'l2')
{
l2();
}
Or you could use a riskier approach and call the function name directly from the input.
$functionName = $_GET['function'];
$functionName();
Edit:
Or you could use a switch statement:
switch($_GET['function'])
{
case 'l1':
l1();
break;
case 'l2':
l2();
break;
case 'l3':
l3();
break;
}

simple php define in drupal template

I have next to no experience with php as a language, and am running it a little problem in producing a Drupal theme. What I need is to execute a function once, that will return a Boolean, then use that Boolean throughout the template.
Here is what I have so far:
html.tpl.php->
<?php
function testMobile(){
return false;
}
define('isMobile', testMobile());
?>
...
<?php
if(!isMobile){
echo '<h1>NOT MOBILE</h1>';
}else{
echo '<h1>IS MOBILE</h1>';
}
?>
page.tpl.php->
<?php
if(!isMobile){
echo '<h1>IS DESKTOP</h1>';
}else{
echo '<h1>NOT DESKTOP</h1>';
}
?>
In the drupal output I get this ->
NOT MOBILE
NOT DESKTOP
along with this error message:
Notice: Use of undefined constant isMobile - assumed 'isMobile' in include() (line 77 of /Users/#/#/#/sites/all/themes/#/templates/page.tpl.php).
what am I doing wrong here? How can I most easily achieve my goal?
It seems that the defined variable is falling out of the scope of the template file. You can simply solve this by using a session variable.
Below is a code sample ...
session_start(); // not necessary with drupal
$_SESSION['isMobile'] = testMobile();
function testMobile(){
return false;
}
In your template you can add following...
<?php
if(!$_SESSION['isMobile']){
echo '<h1>IS DESKTOP</h1>';
}else{
echo '<h1>NOT DESKTOP</h1>';
}
?>
Try to define variable in template.php in hook_theme_preprocess_page(&$vars, $hook).
So template.php can looks follow way:
function testMobile(){
return false;
}
function YOURTHEME_theme_preprocess_page(&$vars, $hook) {
$vars['isMobile'] = testMobile();
}
And page.tpl.php
<?php
if(!$isMobile){
echo '<h1>IS DESKTOP</h1>';
}else{
echo '<h1>NOT DESKTOP</h1>';
}
?>

Creating multilanguage homepage

so im making homepage with three languages.
I am using switch method, here is code -
public function languages()
{
if (isset($_GET['lang']) && $_GET['lang'] != '')
{
$_SESSION['lang'] = $_GET['lang'];
}
else
{
$_SESSION['lang'] = 'en_EN';
}
switch($_SESSION['lang'])
{
case 'en_EN': require_once('language/lang.eng.php');break;
case 'lv_LV': require_once('language/lang.lv.php');break;
case 'ru_RU': require_once('language/lang.ru.php');break;
default: require_once('language/lang.eng.php');
}
}
public function translate($txt)
{
global $text;
return $text[$txt];
}
and it should display in index.php file like this -
<?php $index->translate('search'); ?>
but the problem is that it shows no errors, no notices, no warnings and no translated or default text.
I included function languages() , maybe you can help me with this problem?
EDIT:
im calling $language at start of index.php file - <?php require_once('class.index.php'); $index = new index; $index->languages(); ?> and $text is defined in lang.eng.php; lang.lv.php and lang.ru.php file.
Since you're using a class, I think it's better to use properties instead of globals, it will be easier in future mantainance. Create a class variable holding $text and use that
class Index {
var $text;
public function languages()
{
require(".....");
$this->text = $text;
}
public function translate($txt)
{
if(isset($this->text[$txt]))
{
return $this->text[$txt];
}
else
{
return "no translation";
}
}
}
$index = new Index;
$index->languages();
echo $index->translate('search');
type
<?php echo $index->translate('search'); ?>
Check first whether the session is initialized or not and also place the languages() function call before the translate so that it loads the language prior to translation and also put error_reporting(E_ALL) at top so that any error suppresion will be be removed and also put echo the returned result of translate statement

how to use PHP magic constants from other classes?

i have created below function in a file info.php to debug variable & data during page load
class Info {
static function watch($what,$msg='',$more=FALSE)
{
echo "<hr/>";
echo "<br/>".$msg.":";
if( is_array($what) || is_object($what) )
{
echo "<pre>";
print_r($what);
echo "</pre>";
}
else{
echo $what;
}
if($more)
{
echo "<br/>METHOD:".__METHOD__;
echo "<br/>LINE:".__LINE__;
}
}
}
now i call this method from another page index.php, where i inculded info.php
in this file i want to debug POST array, so i write below code
class Testpost {
__construct() { // some basic intializtion }
function getPostdata($postarray) {
$postarray=$_POST;
Info::watch($postarray,'POST ARRAY', TRUE);
}
everything is working fine but method and LINE appears below
METHOD:Info::watch();
LINE:17 // ( where this code is written in Info class)
but i wantbelow to display
METHOD: Testpost::gtPostdata()
LINE:5( where this function is called in Testpost class)
so how do i do that if i put$more=TRUE in watch() then method and line number should be diaply from the class where it is called.
can i use self:: or parent::in watch method?? or something else
please suggest me how to call magic constants from other classes or is there any other method to debug varaibale?? ( please dont suggest to use Xdebug or any other tools)
You have to use the debug_backtrace() php function.
You also have below the solution to your problem. Enjoy! :)
<?php
class Info {
static function watch($what,$msg='',$more=FALSE)
{
echo "<hr/>";
echo "<br/>".$msg.":";
if( is_array($what) || is_object($what) )
{
echo "<pre>";
print_r($what);
echo "</pre>";
}
else{
echo $what;
}
if($more)
{
$backtrace = debug_backtrace();
if (isset($backtrace[1]))
{
echo "<br/>METHOD:".$backtrace[1]['function'];
echo "<br/>LINE:".$backtrace[1]['line'];
}
}
}
}
You can not use those constants from that scope. Check out the function debug_backtrace() instead. If it gives you too much info, try to parse it.
debug_bactrace is the only way you could totally automate this, but it's a "heavy-duty" function.... very slow to execute, and needs parsing to extract the required information. It might seem cumbersome, but a far better solution is to pass the necessary information to your Info::watch method:
class Info {
static function watch($whereClass,$whereLine,$what,$msg='',$more=FALSE)
{
echo "<hr/>";
echo "<br/>".$msg.":";
if( is_array($what) || is_object($what) )
{
echo "<pre>";
print_r($what);
echo "</pre>";
}
else{
echo $what;
}
if($more)
{
echo "<br/>METHOD:".$whereClass;
echo "<br/>LINE:".$whereLine;
}
}
}
now i call this method from another page index.php, where i inculded info.php
class Testpost {
__construct() { // some basic intializtion }
function getPostdata($postarray) {
$postarray=$_POST;
Info::watch(__METHOD__,__LINE__,$postarray,'POST ARRAY', TRUE);
}

Categories