function and not using Global - php

Hi I was wondering if someone could help me out. Ive tried Global but this is not what I want.
$i= 0
if(.....){
echo "In this part";
$i = 1;
}
else{
echo "........";
$i = 2;
}
function process(){
echo $i;
}

Wouldn't it be better to just pass the stuff you need inside the method, as argument?
process($i);
function process($i){
echo $i;
}

Inside the function also you should declare the global variable.
function process(){
global $i;
echo $i;
}

I tested this and it worked for me the way i believe you would like it to work
$i= 0;
if(1==2){
echo "In this part";
$i = 1;
}
else{
echo "........";
$i = 2;
}
function process(){
global $i;
echo $i;
}
process();

I think you are defining the function and not calling it after the if-else block. you can define it anywhere but call this function after the if-else block simply like
if(....){
echo "In this part";
$i = 1;
}
else{
echo "........";
$i = 2;
}
process();

Sure it will work with $_GLOBALS. Try this code by playing with $var variable
<?php
$i= 0;
$var=0;
if($var==0){
echo "In this part";
$GLOBALS["i"]=1;
}
else{
echo "........";
$GLOBALS["i"]=2;
}
process();
function process(){
echo $GLOBALS["i"];
}
?>

Related

Not getting dynamic variable names PHP

I want to print the values of variable but it prints 12 only
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++)
{
echo $str.$i;
}
This is a better practice:
<?php
$str = array("abhishek","ashish");
for($i=0; $i <= 2; $i++)
{
echo $str[$i].'<br>';
}
?>
Do this:
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++){
echo ${"str".$i};
}

How to echoing the variable and loop?

I want to make in html page like this below:
But, I don't want to write them all one by one, because the number(1, 2, 3) are from $number variable and it can be more than 3.
Use a for loop:
for ($i = 1; $i <= $number; $i++) {
echo "<a href='$i'";
if ($i == $currentvid) {
echo " class='currentvid'";
}
echo "></a>";
}
Try this:
<?php
$href="1";
for($i=1;$i<4;$i++){
if($href==$i){
echo "$i";
}else{
echo "$i";
}
}
?>
$i = 1;
foreach ($number as $k)
{
if($i == 1)
{
echo "$i";
}
else
{
echo "$i";
}
}
}

php - variable iteration in function

I wanted to have a result like this
50+2+2+2+2 = 58
But I'm getting these kind of result
50
2
2
2
2
these are my codes.
<?php
$height = 50;
echo $height;
function hey($x)
{
return $height += $x;
}
$i = 4;
while($i != 0)
{
echo "<br>".hey(2);
$i--;
}
?>
Please take note that the location of my variable and loop must be and really meant at that position.
what do i need to change on my code. I am new in using php functions.
thanks for the help.
You can use a global variable like this try:
function hey($x)
{
global $height;
return $height += $x;
}
And print the variable height only after the called function.
If you don't put global before the variable inside the function it the seems that you create a new variable inside your function. With global you tell to the server to take the variable that you have created outside the function
In this function:
function hey($x)
{
return $height += $x;
}
$height is not in scope, so it is undefined. You should pass it in:
function hey($x, $height)
{
return $height += $x;
}
Then call it like this:
hey(2, $height);
this is scope problem :
function hey($x)
{
global $height;
return $height += $x;
}
Change to:
global $height;
then
while($i != 0)
{
echo "+".hey(2);
$i--;
}
echo "=" . $height;
I don't understand that you want, but if you need output like that, please try this code..
br mean go to bottom, so I delete it.
<?php
$height = 50;
echo $height;
function hey($x)
{
echo " + $x";
return $x;
}
$i = 4;
while($i != 0)
{
$height += hey(2);
$i--;
}
echo " = $height";
?>
This is online demo: http://phpfiddle.org/main/code/6h1-x5z
<?php
function getResult($height = 50, $increment = 2, $times = 4){
echo $height."+";
$total = 0;
for ($i = 0; $i < $times; $i++){
$total += $increment;
if ($i != ($times-1)){
echo $increment."+";
}
else{
echo $increment." = ".($height+$total);
}
}
}
//usage
getResult(50,2,4);
//The print out: 50+2+2+2+2 = 58
?>

in PHP, is it possible to put function into a variable and let the string into the the variable print first?

I have a question: (I've already searched it on google but i couldn't find any answer).
Is it possible that the string in the variable to run first ?
For example :
<?php
function example($times) {
for($i = 4; $i < $times; $i++) echo $i;
}
$var = example(10);
echo "3$var";
?>
and this code prints :
4567893
collect result as variable inside function and return it.
<?php
function example($times) {
$result='';
for($i=4;$i<$times;$i++) $result.=$i;
return $result;
}
$var=example(10);
echo "$var"."3";
other way, only in case you can't control output of function or it have many html markups
is use output buffer capture:
<?php
function example($times) {
for($i=4;$i<$times;$i++) echo $i;
}
ob_start();
example(10);
$var=ob_get_clean;
echo "$var".3;
more info on php.net
You need to understand the difference between echo and return.
Your function example will echo output, directly. The value of $var is NULL, which displays nothing.
So what you are doing is actually the same as this:
echo 4; // in example()
echo 5; // in example()
echo 6; // in example()
echo 7; // in example()
echo 8; // in example()
echo 9; // in example()
echo "3"; // $var == '';
If you want to collect the output, write example like this:
function example($times) {
$numberstring = '';
for($i = 4; $i < $times; $i++) {
$numberstring .= $i;
}
return $numberstring;
}
Try:
function example($times) {
$str='';
for($i = 4; $i < $times; $i++)
$str .=$i;
return $str;
}
$var = example(10);
echo $var."3";

Custom Break Command/Function with create_function() in PHP

I'm wondering if it is possible in PHP to create a custom break command/function.
For example,
<?php
$custombreak = create_function('$a', 'if ($a == 3) break 1;');
$i = 0;
do {
echo $i . '<br />';
$i++;
$custombreak($i); //<-- I'd like to break the loop with a custom function.
// if ($i==3) //<-- this is not what I'm looking for
// break;
} while ($i < 10);
?>
This is not valid PHP code but I hope you get what I'm trying to say. I'd like to escape the loop with the function.
Even if you create normal function
function foo() {
break 2; // this is not valid
}
while(true) {
foo();
}
that doesn't work. This is what programmers do:
function foo() {
return true;
}
while(true) {
if(foo()) break;
}
So your code would be..
http://codepad.org/lztnGflZ
<?php
function control($a){
if($a==3)
return false;
return true;
}
$i = 0;
do {
echo $i . '<br />';
$i++;
} while ( ($i < 10) && (control($i)) );
?>
is it an answer for this question ?
I found a way with Exceptions.
$i = 0;
try {
do {
$i++;
echo $i . '<br />';
custombreak($i);
} while ($i <= 10);
} catch (Exception $e) {}
function custombreak($i) {
if ($i > 3) throw new Exception("");
}
Thanks all.
you can use this control in while loop. Don't need to write a function i think.
<?php
$i = 0;
do {
echo $i . '<br />';
$i++;
} while ( ($i < 10) && ($i!=3) );
?>
You know multiple controls don't you ?

Categories